I have written a simple Tic Tac Toe game in WPC C#. I am just a beginner and learning. Could you tell me if there is something wrong with my code / could be done better? I am pretty much satisfied with how it works, but I´d like to know if I made some things, that could have been done better. Thanks in advance.
In the window there are 9 buttons, 1 reset button, and a textBlock for messages.
MainWindow.xaml.cs Class
namespace TicTacToe
{
public partial class MainWindow : Window
{
private Button[] buttons;
private TextBlock textAlert;
private Button resetButt;
private TicTacToeGame game;
public MainWindow()
{
InitializeComponent();
// sends array of buttons, alertTextBlock and resetButton to TicTacToe game
textAlert = AlertTextBlock;
buttons = new Button[9];
buttons[0] = button1x1;
buttons[1] = button1x2;
buttons[2] = button1x3;
buttons[3] = button2x1;
buttons[4] = button2x2;
buttons[5] = button2x3;
buttons[6] = button3x1;
buttons[7] = button3x2;
buttons[8] = button3x3;
resetButt = resetButton;
game = new TicTacToeGame(buttons, textAlert, resetButt);
}
private void button_Click(object sender, RoutedEventArgs e)
{
Button clicked = (Button)sender; //clicked button
if (game.getPlayer1Turn())
{
clicked.Content = "X"; //sets button to X
}
else
{
clicked.Content = "O"; //sets button to O
}
clicked.IsEnabled = false; //disables clicked button, so it doesn´t change
game.nextTurn(); //proceeds to next turn
}
private void resetButton_click(object sender, RoutedEventArgs e)
{
game.reset();
}
}
}
TicTacToe.cs class
namespace TicTacToe
{
class TicTacToeGame
{
private bool player1Turn;
private int turn;
private TextBlock alertText;
private Button[] buttons;
private Button resetButton;
private bool gameFinished;
private int[][] winningPos = new int[8][]
{
new int[3] {0,1,2},
new int[3] {3,4,5},
new int[3] {6,7,8},
new int[3] {0,3,6},
new int[3] {1,4,7},
new int[3] {2,5,8},
new int[3] {0,4,8},
new int[3] {2,4,6}
};
/* constructor */
public TicTacToeGame(Button[] buttons, TextBlock alertText, Button resetButton)
{
player1Turn = false;
turn = 0;
this.alertText = alertText;
this.buttons = buttons;
this.resetButton = resetButton;
gameFinished = false;
nextTurn();
}
/* tells who is on turn true - player1; false - player2*/
public bool getPlayer1Turn()
{
return player1Turn;
}
/* method nextTurn(), pretty straight forward */
public void nextTurn()
{
player1Turn = !player1Turn;
turn++;
if(player1Turn)
alertText.Text = "It is player 1´s turn. (X). Turn: " + turn;
else
alertText.Text = "It is player 2´s turn. (O) Turn: " + turn;
Validate();
}
/* Check if one of the player won */
private void Validate()
{
string content = "";
string winner = "";
foreach (int[] p in winningPos)
{
for (int i = 0; i <= 2; i++)
{
if(i == 0)
content = buttons[p[i]].Content.ToString();
if (content != buttons[p[i]].Content.ToString())
break;
if (content == "")
break;
if (i == 2 && content == buttons[p[i]].Content.ToString())
{
gameFinished = true;
winner = content;
}
}
}
if (gameFinished)
{
if(winner == "X")
alertText.Text = "Player 1 wins! Congratulations!";
else if (winner == "O")
alertText.Text = "Player 2 wins! Congratulations!";
disableAll();
resetButton.IsEnabled = true;
}
else if (turn == 10 && !gameFinished)
{
alertText.Text = "Tie!";
disableAll();
resetButton.IsEnabled = true;
}
}
/* disables all the buttons (when one of the player wins) */
private void disableAll()
{
foreach (Button butt in buttons)
butt.IsEnabled = false;
}
/* method to reset the game, allowing to play again */
public void reset()
{
foreach (Button butt in buttons)
{
butt.IsEnabled = true;
butt.Content = "";
}
player1Turn = false;
turn = 0;
gameFinished = false;
resetButton.IsEnabled = false;
nextTurn();
}
}
}