Write a console Gobang program in C #. Learn the more interesting programs that are encountered in C #.

Source: Internet
Author: User

First, define the number of rows of the chessboard in the main method, int hang,lie = 15; Then define the array, string[,] Qipan = new String[hangshu,lieshu];

Then write a way of playing chess (which uses * to represent white, #代表黑棋)

static void Xiaqi (string[,] qipan,int hang,int lie,bool baizixia)

{

Qipan[hang, lie] = Baizixia? "*":"#";

}

? "*":"#"; This is a trinocular operator, the condition is the true result of the question mark, the condition is false, the result is the colon after, because to determine the order of the White sunspot chess, so in the method to define a bool value, and then use the trinocular operator to judge. This makes it very convenient to judge who is under.

Determine who will write a method to interact with the user after the next, so:

The static void Jiaohu (bool baizixia, out int hanghao, out int Liehao) defines a bool variable because there are only two cases in black and white, and the next one is to write an if statement

{

if (Baizixia)

{

Console.WriteLine ("Please enter the Lazi line number under the white Son");

Hanghao = Int. Parse (Console.ReadLine ());

Console.WriteLine ("Please enter the Lazi column number under the white Son");

Liehao = Int. Parse (Console.ReadLine ());

}

Else

{

Console.WriteLine ("Please enter the Lazi line number under the sunspot");

Hanghao = Int. Parse (Console.ReadLine ());

Console.WriteLine ("Please enter the Lazi column number under the Sunspot");

Liehao = Int. Parse (Console.ReadLine ());

}

This way, user interaction is finished.

Then we need a method to check the Lazi, to judge where the next place can not be again and whether it is beyond the board bounds, with the IF statement to judge

static bool Jianchaluozi (string[,] qipan, int hanghao, int liehao, int hangshu, int lieshu)

{

if (Hanghao >= Hangshu | | Liehao >= lieshu)
{
Console.WriteLine ("The position is no longer within the board, please re-lazi");
return false;
}

Else
{
if (Qipan[hanghao, liehao] = = "+")//Empty position
{
Return true;//Compliance Rules
}
Else
{
Console.WriteLine ("The position already has a pawn, please re-lazi");
Return false;//does not conform to the rules
}
}

}

So the method of judging the position of the pawn is finished.

Then make a checkerboard interface, output the chessboard, and use the For loop to control the output of the checkerboard rows.

static void Xianshiqipan (string[,] qipan, int hangshu, int lieshu)
{
Console.WriteLine ("0 1 2 3 4 5 6 7 8 9 10 11 12 13 14");
for (int i = 0; i < Hangshu; i++)
{
Console.Write (i < i + "line:": i + "line:");
for (int j = 0; J < Lieshu; J + +)
{
Console.Write (Qipan[i, J] + "");
}
Console.WriteLine ();
}
}

Next to write the method of judging the outcome, judging the outcome of four kinds, one is 5, one is vertical lines 5, then left oblique line 5, right oblique line 5. Can write four if statements

static int Panduanshengfu (string[,] qipan, int hangshu, int lieshu)
{

for (int i = 0; i < Hangshu; i++)
{
for (int j = 0; J < Lieshu-4; J + +)
{

#region Rampage
if (J < lieshu-4)
{
if (Qipan[i, j] = = "#" && Qipan[i, j + 1] = = "#" && Qipan[i, j + 2] = = "#" && Qipan[i, j + 3] = = " # "&& Qipan[i, j + 4] = =" # ")
{
Rampant sunspots win
return-1;
}
if (Qipan[i, j] = = "*" && Qipan[i, j + 1] = = "*" && Qipan[i, j + 2] = = "*" && Qipan[i, j + 3] = = " * "&& Qipan[i, j + 4] = =" * ")
{
Horizontal white son win
return 1;
}

}
#endregion

#region Vertical Lines
if (i < hangshu-4)
{
          if (qipan[i, j] = = "#" && Qipa N[i + 1, j] = = "#" && Qipan[i + 2, j] = = "#" && Qipan[i + 3, j] = = "#" && Qipan[i + 4, j] = = "#") br>            {
             //Vertical lines sunspot win
  &NBSP ;           return-1;
             }
          if (qipan[i, j] = = "*" && Qipan[i + 1, j] = = "*" && Qipan[i + 2, j] = = "*" && Qipan[i + 3, j] = = "*" && Qipan[i + 4, j] = = "*")
             {
               //Vertical lines white win
            &N Bsp  return 1;
             }
}
#endregion

#region Right Oblique
if (i < hangshu-4 && J < lieshu-4)
{
          if (Qipan [I, j] = = "#" && Qipan[i + 1, j + 1] = = "#" && Qipan[i + 2, j + 2] = = "#" && Qipan[i + 3, j + 3] = = "#" && Qipan[i + 4, j + 4] = = "#")
          {
             //Sunspot win
              return-1;
             }
          if (qipan[i, j] = = "*" && Qipan[i + 1, j + 1] = = "*" && Qipan[i + 2, j + 2] = = "*" && Qipan[i + 3, j + 3] = = "*" && qipan[i + 4, j + 4] = = "*")
            {
             //White Win
            return 1;
           
}
#endregion

#region Left Oblique
if (i < hangshu-4 && J >= 4)
{
          &NBSP;IF (qipan[i , j] = = "#" && qipan[i + 1, j-1] = = "#" && Qipan[i + 2, j-2] = = "#" && Qipan[i + 3, j-3] = = "#" && Qipan[i + 4, j-4] = = "#")
            {
        & nbsp    //Vertical lines sunspot win
              return-1;
             }
            if (qipan[i, j] = = "*" & ;& qipan[i + 1, j-1] = = "*" && Qipan[i + 2, j-2] = = "*" && Qipan[i + 3, j-3] = = "*" && Q Ipan[i + 4, j-4] = = "*")
             {
          &NBS P  //Vertical lines White wins
              return 1;
             
}
#endregion

}

}
return 0;
}

Then let the chessboard output with the + number,

static void Initqipan (string[,] qipan, int hangshu, int lieshu)
{
for (int i = 0; i < Hangshu; i++)
{
for (int j = 0; J < Lieshu; J + +)
{
Qipan[i, J] = "+";
}
}
}

Finally write a method to control the game

static void Gamemanager (string[,] qipan, int hangshu, int lieshu)
{
//Initialize First
Initqipan (Qipan, Hangshu, Lieshu);
//Display checkerboard
Xianshiqipan (Qipan, Hangshu, Lieshu);
loop control rotate Chess
bool Baizixia = true;
while (true)
{
//interactive
int Hanghao, Liehao;
Jiaohu (Baizixia, out Hanghao, out Liehao);
//Check rules
if (Jianchaluozi (Qipan, Hanghao, Liehao, Hangshu, Lieshu))
{
//chess
Xiaqi (Qipan, Hanghao, Liehao , Baizixia);
//Display checkerboard
Xianshiqipan (Qipan, Hangshu, Lieshu);
Judgment winner
Int shengfu = Panduanshengfu (Qipan, Hangshu, Lieshu);
if (Shengfu = =-1)
{
Console.WriteLine ("Black wins");
Initqipan (Qipan, Hangshu, Lieshu);
Xianshiqipan (Qipan, Hangshu, Lieshu);
}
Else if (Shengfu = = 1)
{
Console.WriteLine ("White wins");
Initqipan (Qipan, Hangshu, Lieshu);
Xianshiqipan (Qipan, Hangshu, Lieshu);
}
}

Else
{
Console.WriteLine ("Lazi does not conform to the rules, please re-lazi");
}
//exchange player
Baizixia =!baizixia;
}
}

After writing this method, the game method is called in the Main method, and it is finished.

This Gobang disk is I am learning C # Process of the first difficulty than the larger program, under the guidance of the teacher step by step down, in the blog to re-disk a bit, or there are many do not understand the place, the Gobang disk fused for loops, if statements, methods of invocation, as well as a variety of methods of writing. is a relatively complete job of learning C #. I will continue to work hard to learn this gobang well.

Write a console Gobang program in C #. Learn the more interesting programs that are encountered in C #.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.