Created
July 30, 2019 13:32
-
-
Save BMU-Verlag/369e27da90098a287ebde05f490d9f6f to your computer and use it in GitHub Desktop.
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, INotifyPropertyChanged | |
{ | |
public event PropertyChangedEventHandler PropertyChanged; | |
private const string PLAYER_1_SYMBOL = "X"; | |
private const string PLAYER_2_SYMBOL = "O"; | |
private const int FIELD_SIZE = 3; | |
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; | |
public bool GameActive | |
{ | |
get { return gameActive; } | |
set | |
{ | |
gameActive = value; | |
OnPropertyChanged("GameActive"); | |
} | |
} | |
private int fieldsMarked = 0; | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
DataContext = this; | |
GameInformation.Content = "X ist am Zug"; | |
} | |
protected void OnPropertyChanged(string propertyName) | |
{ | |
var handler = PropertyChanged; | |
if (handler != null) | |
{ | |
handler(this, new PropertyChangedEventArgs(propertyName)); | |
} | |
} | |
private string GetCurrentSymbol() | |
{ | |
return currentPlayer == Player.Player1 ? PLAYER_1_SYMBOL : PLAYER_2_SYMBOL; | |
} | |
private void Button_Click(object sender, EventArgs e) | |
{ | |
var button = (Button)sender; | |
int column = Grid.GetColumn(button); | |
int row = Grid.GetRow(button); | |
if (FieldIsLegal(column, row)) | |
{ | |
MarkField(button, column, row); | |
var playerWon = CheckForWinner(); | |
if (playerWon) | |
{ | |
EndGame(); | |
} | |
else if (fieldsMarked >= FIELD_SIZE * FIELD_SIZE) | |
{ | |
SetGameIsDraw(); | |
} | |
else | |
{ | |
ContinueGame(); | |
} | |
} | |
} | |
private void EndGame() | |
{ | |
GameInformation.Content = $"{GetCurrentSymbol()} hat gewonnen!"; | |
GameActive = false; | |
} | |
private void SetGameIsDraw() | |
{ | |
GameInformation.Content = "Unentschieden!"; | |
GameActive = false; | |
} | |
private void ContinueGame() | |
{ | |
SwitchPlayer(); | |
GameInformation.Content = $"{GetCurrentSymbol()} ist am Zug"; | |
} | |
private bool FieldIsLegal(int column, int row) | |
{ | |
return GameActive && fields[column][row] == Player.None; | |
} | |
private void MarkField(Button button, int column, int row) | |
{ | |
button.Content = GetCurrentSymbol(); | |
fields[column][row] = currentPlayer; | |
fieldsMarked++; | |
} | |
private void SwitchPlayer() | |
{ | |
if (currentPlayer == Player.Player1) | |
{ | |
currentPlayer = Player.Player2; | |
} | |
else | |
{ | |
currentPlayer = Player.Player1; | |
} | |
} | |
private bool CheckForWinner() | |
{ | |
return CheckRows() || CheckColumns() || CheckDiagonals(); | |
} | |
private bool CheckRows() | |
{ | |
foreach (List<Player> row in fields) | |
{ | |
if (row[0] != Player.None && row[0] == row[1] && row[1] == row[2]) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
private bool CheckColumns() | |
{ | |
for (var column_index = 0; column_index < FIELD_SIZE; 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; | |
} | |
} | |
return false; | |
} | |
private bool CheckDiagonals() | |
{ | |
if (fields[0][0] != Player.None && fields[0][0] == fields[1][1] | |
&& fields[1][1] == fields[2][2]) | |
{ | |
return true; | |
} | |
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 < FIELD_SIZE; x++) | |
{ | |
for (var y = 0; y < FIELD_SIZE; 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