This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace TicTacToe | |
{ | |
public partial class MainWindow : Window | |
{ | |
private List<List<Player>> fields = new List<List<Player>>() | |
{ | |
new List<Player>() {Player.None, Player.None, Player.None}, | |
new List<Player>() {Player.None, Player.None, Player.None}, | |
new List<Player>() {Player.None, Player.None, Player.None}, | |
}; | |
private Player currentPlayer = Player.Player1; | |
private bool gameActive = true; | |
private int fieldsMarked = 0; | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
GameInformation.Content = "X ist am Zug"; | |
} | |
private void Button_Click(object sender, EventArgs e) | |
{ | |
var button = (Button)sender; | |
int column = Grid.GetColumn(button); | |
int row = Grid.GetRow(button); | |
if (gameActive && fields[column][row] == Player.None) | |
{ | |
var currentSymbol = ""; | |
if (currentPlayer == Player.Player1) | |
{ | |
currentSymbol = "X"; | |
} | |
else | |
{ | |
currentSymbol = "O"; | |
} | |
button.Content = currentSymbol; | |
fields[column][row] = currentPlayer; | |
fieldsMarked++; | |
var playerWon = CheckForWinner(); | |
if (playerWon) | |
{ | |
GameInformation.Content = $"{currentSymbol} hat gewonnen!"; | |
gameActive = false; | |
} | |
else | |
{ | |
if (fieldsMarked >= 9) | |
{ | |
GameInformation.Content = "Unentschieden!"; | |
gameActive = false; | |
} | |
else if (currentPlayer == Player.Player1) | |
{ | |
currentPlayer = Player.Player2; | |
GameInformation.Content = "O ist am Zug"; | |
} | |
else | |
{ | |
currentPlayer = Player.Player1; | |
GameInformation.Content = "X ist am Zug"; | |
} | |
} | |
} | |
} | |
private bool CheckForWinner() | |
{ | |
// Check rows | |
foreach (List<Player> row in fields) | |
{ | |
if (row[0] != Player.None && row[0] == row[1] && row[1] == row[2]) | |
{ | |
return true; | |
} | |
} | |
// Check columns | |
for (var column_index = 0; column_index < 3; column_index++) | |
{ | |
if (fields[0][column_index] != Player.None | |
&& fields[0][column_index] == fields[1][column_index] | |
&& fields[1][column_index] == fields[2][column_index]) | |
{ | |
return true; | |
} | |
} | |
// Check left-right diagonal | |
if (fields[0][0] != Player.None && fields[0][0] == fields[1][1] | |
&& fields[1][1] == fields[2][2]) | |
{ | |
return true; | |
} | |
// Check right-left diagonal | |
if (fields[0][2] != Player.None && fields[1][1] == fields[2][0] | |
&& fields[1][1] == fields[0][2]) | |
{ | |
return true; | |
} | |
return false; | |
} | |
private void StartButton_Click(object sender, EventArgs e) | |
{ | |
gameActive = true; | |
for (var x = 0; x < 3; x++) | |
{ | |
for (var y = 0; y < 3; y++) | |
{ | |
fields[x][y] = Player.None; | |
} | |
} | |
Button0_0.Content = ""; | |
Button0_1.Content = ""; | |
Button0_2.Content = ""; | |
Button1_0.Content = ""; | |
Button1_1.Content = ""; | |
Button1_2.Content = ""; | |
Button2_0.Content = ""; | |
Button2_1.Content = ""; | |
Button2_2.Content = ""; | |
fieldsMarked = 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment