/// <summary> /// Checks if the game is finished /// </summary> private void CheckGame() { CellInfo[] Row1 = new CellInfo[3]; CellInfo[] Row2 = new CellInfo[3]; CellInfo[] Row3 = new CellInfo[3]; CellInfo[] Col1 = new CellInfo[3]; CellInfo[] Col2 = new CellInfo[3]; CellInfo[] Col3 = new CellInfo[3]; CellInfo[] Dia1 = new CellInfo[3]; CellInfo[] Dia2 = new CellInfo[3]; //Winner Text String. Contains if a player won or its a tie String WinText = ""; for (int i = 0; i < 3; i++) { Row1[i] = GameResult[0, i]; Row2[i] = GameResult[1, i]; Row3[i] = GameResult[2, i]; Col1[i] = GameResult[i, 0]; Col2[i] = GameResult[i, 1]; Col3[i] = GameResult[i, 2]; Dia1[i] = GameResult[i, i]; Dia2[i] = GameResult[i, 2 - i]; } //Check Row if (Row1.All(Element => Element == CellInfo.X) || Row1.All(Element => Element == CellInfo.O)) { WinText = Row1[0] == CellInfo.X ? "Player 1 Wins!" : "Player 2 Wins!"; Button1.Background = Button4.Background = Button7.Background = Brushes.LightGreen; GameEnded = true; } if (Row2.All(Element => Element == CellInfo.X) || Row2.All(Element => Element == CellInfo.O)) { WinText = Row2[0] == CellInfo.X ? "Player 1 Wins!" : "Player 2 Wins!"; Button2.Background = Button5.Background = Button8.Background = Brushes.LightGreen; GameEnded = true; } if (Row3.All(Element => Element == CellInfo.X) || Row3.All(Element => Element == CellInfo.O)) { WinText = Row3[0] == CellInfo.X ? "Player 1 Wins!" : "Player 2 Wins!"; Button3.Background = Button6.Background = Button9.Background = Brushes.LightGreen; GameEnded = true; } //Check Coloumn if (Col1.All(Element => Element == CellInfo.X) || Col1.All(Element => Element == CellInfo.O)) { WinText = Col1[0] == CellInfo.X ? "Player 1 Wins!" : "Player 2 Wins!"; Button1.Background = Button2.Background = Button3.Background = Brushes.LightGreen; GameEnded = true; } if (Col2.All(Element => Element == CellInfo.X) || Col2.All(Element => Element == CellInfo.O)) { WinText = Col2[0] == CellInfo.X ? "Player 1 Wins!" : "Player 2 Wins!"; Button4.Background = Button5.Background = Button6.Background = Brushes.LightGreen; GameEnded = true; } if (Col3.All(Element => Element == CellInfo.X) || Col3.All(Element => Element == CellInfo.O)) { WinText = Col3[0] == CellInfo.X ? "Player 1 Wins!" : "Player 2 Wins!"; Button7.Background = Button8.Background = Button9.Background = Brushes.LightGreen; GameEnded = true; } //Check Diagonal if (Dia1.All(Element => Element == CellInfo.X) || Dia1.All(Element => Element == CellInfo.O)) { WinText = Dia1[0] == CellInfo.X ? "Player 1 Wins!" : "Player 2 Wins!"; Button1.Background = Button5.Background = Button9.Background = Brushes.LightGreen; GameEnded = true; } if (Dia2.All(Element => Element == CellInfo.X) || Dia2.All(Element => Element == CellInfo.O)) { WinText = Dia2[0] == CellInfo.X ? "Player 1 Wins!" : "Player 2 Wins!"; Button3.Background = Button5.Background = Button7.Background = Brushes.LightGreen; GameEnded = true; } if (TurnsPlayed == 9) { GameEnded = true; if (WinText == "") { WinText = "Tie Game!"; } } if (GameEnded) { MessageBox.Show(WinText); StartGame(); } }