So I am writing a tic tac toe simulator. One where it is only the computer playing, there is no human player. I have the program written but I am having some issues with an error and some graphical things.
1. I'm receiving and error on the return null; statement it says it is an invalid token. 2. My output on the form is made of labels but my labels are not in a grid layout and with very small text, I can't figure out how to make them bigger. 3. I want to add lines on the form like you would see when someone hand draws the game, however I am unsure how to get those drawn on the windows form. It seemed that I was also not consistently declaring a winner with the message box, and it seems that I've never see X win...it could just be a coincidence. Any and all help is appreciated. Thanks in Advance!
namespace kjblkblt
{
public partial class TTT_frm : Form
{
private Random Generated = new Random();
private Point LastMove;
private const int Board_Size = 3;
const int Squares = 9;
char[] XorO = new char[Squares];
public TTT_frm()
{
InitializeComponent();
}
private void TTT_frm_Load(object sender, EventArgs e)
{
}
private void Sim_btn_Click(object sender, EventArgs e)
{
//Erase contents of Array
for (int i = 0; i < Squares; i++)
{
XorO[i] = ' ';
}
//Erase contents of Results label
Result_lbl.Text = " ";
//Set fist and second player X's and O's
char FirstPlayer = 'O';
char SecondPlayer = 'X';
//Generate the random numbers for the grid
int[] position = new int[5];
position[0] = Generated.Next(9);
for (int i = 1; i < 5; i++)
{
int temp = Generated.Next(9);
for (int j = 0; j < i; j++)
{
if (temp == position[j])
{
i--;
break;
}
else
{
position[i] = temp;
}
}
}
//Get first players positions
for (int i = 0; i < 5; i++)
{
XorO[position[i]] = FirstPlayer;
}
for (int i = 0; i < Squares; i++)
{
if (XorO[i] != FirstPlayer)
{
XorO[i] = SecondPlayer;
}
}
//Put the X's or O's in the labels
A1_lbl.Text = XorO[0].ToString();
A2_lbl.Text = XorO[1].ToString();
A3_lbl.Text = XorO[2].ToString();
B1_lbl.Text = XorO[3].ToString();
B2_lbl.Text = XorO[4].ToString();
B3_lbl.Text = XorO[5].ToString();
C1_lbl.Text = XorO[6].ToString();
C2_lbl.Text = XorO[7].ToString();
C3_lbl.Text = XorO[8].ToString();
//See Who Won
switch(Winner())
{
case 'O':
Result_lbl.Text = "O Wins!";
break;
case 'x':
Result_lbl.Text = "x Wins!";
break;
case 'T':
Result_lbl.Text = "It Was a Tie!";
break;
}
}
private void GenerateNextStep(char playerChar, char[][] gameBoard)
{
int x;
int y;
if (LastMove ==null)
{
//Is the first move and can place anywhere
x = Generate.Next() % Board_Size;
y = Generate.Next() % Board_Size;
}
else
{
Point p = FindClosest();
x = p.x;
y = p.y;
}
gameBoard[x][y] = playerChar;
LastMove = new Point(x, y);
}
private Point FindClosest()
{
for (int x = LastMove.x - 1; x < x +1; x++)
{
if (x < 0 || x >= Board_Size)
{
continue;
}
if (gameBoard[x][y] == ' ')
{
return new Point(x, y);
}
}
}
return null;
private char Winner()
{
//Return result of game so that a determination can be made
//Returns X, O or T
char winner = ' ';
int Win_Sequence = 0;
//Columns
if (XorO[0].Equals(XorO[3]) && XorO[0].Equals(XorO[6]))
{
Win_Sequence++;
winner = XorO[0];
}
if (XorO[1].Equals(XorO[4]) && XorO[1].Equals(XorO[7]))
{
Win_Sequence++;
winner = XorO[1];
}
if (XorO[2].Equals(XorO[5]) && XorO[2].Equals(XorO[8]))
{
Win_Sequence++;
winner = XorO[2];
}
//Rows
if (XorO[0].Equals(XorO[1]) && XorO[0].Equals(XorO[2]))
{
Win_Sequence++;
winner = XorO[0];
}
if (XorO[3].Equals(XorO[4]) && XorO[3].Equals(XorO[5]))
{
Win_Sequence++;
winner = XorO[3];
}
if (XorO[6].Equals(XorO[7]) && XorO[6].Equals(XorO[8]))
{
Win_Sequence++;
winner = XorO[6];
}
//Diagonal
if (XorO[0].Equals(XorO[4]) && XorO[0].Equals(XorO[8]))
{
Win_Sequence++;
winner = XorO[0];
}
if (XorO[2].Equals(XorO[4]) && XorO[2].Equals(XorO[8]))
{
Win_Sequence++;
winner = XorO[2];
}
if (Win_Sequence == 0 || Win_Sequence > 1)
{
winner = 'T';
}
return winner;
}
private void Exit_btn_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Aucun commentaire:
Enregistrer un commentaire