我制作了一个tictactoe游戏并使用函数来简化代码,我的函数playx和playo似乎也没有正常工作然后我尝试编辑我的代码但是它无法正常工作或者我似乎无法写入正确的网格,有时它写在两个网格第二代码
using System;
namespace tictactoe
{
class MainClass
{
static string[,] game = new string[3, 3];
public static void Main(string[] args)
{
Console.WriteLine("Welcome to tictactoe,please play enter to play ");
Console.Clear();
int n = 0;
while (n < 9)
{
print();
playx();
if (1 == checkifwon())
{
Console.WriteLine("player y won");
}
else if (2 == checkifwon())
{
Console.WriteLine("player x won");
}
print();
playo();
if (1 == checkifwon())
{
Console.WriteLine("player y won");
}
else if (2 == checkifwon())
{
Console.WriteLine("player x won");
}
print();
}
Console.ReadKey();
}
static void print()
{
Console.WriteLine(game[0, 0] + "|" + game[0, 1] + "|" + game[0, 2]);
Console.WriteLine(game[0, 0] + "|" + game[1, 1] + "|" + game[2, 2]);
Console.WriteLine(game[2, 0] + "|" + game[2, 1] + "|" + game[2, 2]);
}
static int checkifwon()
{
if (lineWin("o", game[0, 0], game[0, 1], game[0, 2]) ||
lineWin("o", game[1, 0], game[1, 1], game[1, 2]) ||
lineWin("o", game[2, 0], game[2, 1], game[2, 2]) ||
lineWin("o", game[0, 0], game[0, 1], game[0, 2]) ||
lineWin("o", game[1, 0], game[1, 1], game[1, 2]) ||
lineWin("o", game[2, 0], game[2, 1], game[2, 2]) ||
lineWin("o", game[0, 0], game[1, 1], game[2, 2]) ||
lineWin("o", game[2, 0], game[1, 1], game[0, 2])
)
{
return 1;
}
else if (lineWin("x", game[0, 0], game[0, 1], game[0, 2]) ||
lineWin("x", game[1, 0], game[1, 1], game[1, 2]) ||
lineWin("x", game[2, 0], game[2, 1], game[2, 2]) ||
lineWin("x", game[0, 0], game[0, 1], game[0, 2]) ||
lineWin("x", game[1, 0], game[1, 1], game[1, 2]) ||
lineWin("x", game[2, 0], game[2, 1], game[2, 2]) ||
lineWin("x", game[0, 0], game[1, 1], game[2, 2]) ||
lineWin("x", game[2, 0], game[1, 1], game[0, 2]))
{
return 2;
}
else
{
return 3;
}
}
static bool lineWin(string player, string first, string second, string third)
{
if (first == player && second == player && third == player)
return true;
else
return false;
}
static void Cheat()
{
if (game[0, 0] == "o" && game[0, 1] == "o" && game[0, 2] == "o" ||
game[0, 0] == "o" && game[0, 1] == "o" && game[0, 2] == "o")
{
}
}
static void playx()
{
Console.WriteLine("enter the x coordinate player x");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter the y coordinate");
int b = Convert.ToInt32(Console.ReadLine());
game[a, b] = "x";
}
static void playo()
{
Console.WriteLine("enter the x coordinate player o");
int q = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter the y coordinate");
int c = Convert.ToInt32(Console.ReadLine());
game[q, c] = "o";
}
}
}
第一个代码
using System;
namespace tictactoe
{
class MainClass
{
static string[,] game = new string[3, 3];
public static void Main(string[] args)
{
Console.WriteLine("Welcome to tictactoe; please enter players name?");
string player1 = Console.ReadLine();
Console.WriteLine(" please enter player 2 name?");
string player2 = Console.ReadLine();
Console.Clear();
int n = 0;
while (n < 9)
{
print();
playx();
print();
if (3 == checkifwon())
{
playo();
}
else if (2 == checkifwon())
{
Console.WriteLine("player x won");
}
else
{
Console.WriteLine("player y won");
}
print();
}
Console.ReadKey();
}
static void print()
{
Console.WriteLine(game[0, 0] + "|" + game[0, 1] + "|" + game[0, 2]);
Console.WriteLine(game[0, 0] + "|" + game[1, 1] + "|" + game[2, 2]);
Console.WriteLine(game[2, 0] + "|" + game[2, 1] + "|" + game[2, 2]);
}
static int checkifwon()
{
if (lineWin("o", game[0, 0], game[0, 1], game[0, 2]) ||
lineWin("o", game[1, 0], game[1, 1], game[1, 2]) ||
lineWin("o", game[2, 0], game[2, 1], game[2, 2]) ||
lineWin("o", game[0, 0], game[0, 1], game[0, 2]) ||
lineWin("o", game[1, 0], game[1, 1], game[1, 2]) ||
lineWin("o", game[2, 0], game[2, 1], game[2, 2]) ||
lineWin("o", game[0, 0], game[1, 1], game[2, 2]) ||
lineWin("o", game[2, 0], game[1, 1], game[0, 2])
)
{
return 1;
}
else if (lineWin("x", game[0, 0], game[0, 1], game[0, 2]) ||
lineWin("x", game[1, 0], game[1, 1], game[1, 2]) ||
lineWin("x", game[2, 0], game[2, 1], game[2, 2]) ||
lineWin("x", game[0, 0], game[0, 1], game[0, 2]) ||
lineWin("x", game[1, 0], game[1, 1], game[1, 2]) ||
lineWin("x", game[2, 0], game[2, 1], game[2, 2]) ||
lineWin("x", game[0, 0], game[1, 1], game[2, 2]) ||
lineWin("x", game[2, 0], game[1, 1], game[0, 2]))
{
return 2;
}
else
{
return 3;
}
}
static bool lineWin(string player, string first, string second, string third)
{
if (first == player && second == player && third == player)
return true;
else
return false;
}
static void Cheat()
{
if (game[0, 0] == "o" && game[0, 1] == "o" && game[0, 2] == "o" ||
game[0, 0] == "o" && game[0, 1] == "o" && game[0, 2] == "o")
{
}
}
static void playx()
{
Console.WriteLine("enter the x coordinate player x");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter the y coordinate");
int b = Convert.ToInt32(Console.ReadLine());
if (game[a, b] == "x")
{
Console.WriteLine("already played that grid");
}
else if (game[a, b] == "o")
{
Console.WriteLine("sorry,can not play that grid");
}
else
{
game[a, b] = "x";
}
}
static void playo()
{
Console.WriteLine("enter the x coordinate player x");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter the y coordinate");
int b = Convert.ToInt32(Console.ReadLine());
if (game[a, b] == "o")
{
Console.WriteLine("already played that grid");
}
else if (game[a, b] == "x")
{
Console.WriteLine("sorry,can not play that grid");
}
else
{
game[a, b] = "o";
}
}
}
}
答案 0 :(得分:1)
如果你看看print()
,你会发现你在game[0,0]
的第二行重复game[1,0]
。您也可以在game[2,2]
game[1,2]
您的功能应如下所示:
static void print()
{
Console.WriteLine(game[0, 0] + "|" + game[0, 1] + "|" + game[0, 2]);
Console.WriteLine(game[1, 0] + "|" + game[1, 1] + "|" + game[1, 2]);
Console.WriteLine(game[2, 0] + "|" + game[2, 1] + "|" + game[2, 2]);
}
您认为您看到的错误是因为您的打印值不正确。这就是为什么你说&#34;有时它写在两个网格&#34;有时您在game[0,0]
和game[2,2]
中放置了一个值。