Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- public class Board
- {
- public char[,] Cells = new char[3, 3];
- public int FreePostions { get; set; }
- public Board()
- {
- for (int r = 0; r < 3; r++)
- {
- for (int c = 0; c < 3; c++)
- {
- this.Cells[r, c] = '-';
- }
- }
- this.FreePostions = 9;
- }
- }
- public class TicTacToe
- {
- public static void Main()
- {
- Board board = new Board();
- ReadBoard(board);
- Dictionary<string, int> resultCounts = new Dictionary<string, int>();
- resultCounts.Add("X", 0);
- resultCounts.Add("O", 0);
- resultCounts.Add("Draw", 0);
- char playerOnTurn;
- if (board.FreePostions % 2 == 0)
- {
- playerOnTurn = 'O';
- }
- else
- {
- playerOnTurn = 'X';
- }
- FindResult(board, resultCounts, playerOnTurn);
- PrintResult(resultCounts);
- }
- private static void ReadBoard(Board board)
- {
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- char symbol = (char)Console.Read();
- if (symbol == '-')
- {
- board.Cells[i, j] = symbol;
- }
- else
- {
- board.Cells[i, j] = symbol;
- board.FreePostions--;
- }
- }
- Console.ReadLine();
- }
- }
- private static void FindResult(Board board, Dictionary<string, int> resultCounts, char playerOnTurn)
- {
- // Recursion bottom
- char winner = ' ';
- if (CheckWin(board, out winner))
- {
- if (winner == 'X')
- {
- resultCounts["X"]++;
- return;
- }
- else
- {
- resultCounts["O"]++;
- return;
- }
- }
- if (board.FreePostions == 0)
- {
- resultCounts["Draw"]++;
- return;
- }
- // Recursive call
- for (int row = 0; row < 3; row++)
- {
- for (int col = 0; col < 3; col++)
- {
- if (board.Cells[row,col] == '-')
- {
- board.Cells[row, col] = playerOnTurn;
- board.FreePostions--;
- FindResult(board, resultCounts, SwitchPlayerOnTurn(playerOnTurn));
- board.Cells[row, col] = '-';
- board.FreePostions++;
- }
- }
- }
- }
- private static bool CheckWin(Board board, out char winner)
- {
- bool result = false;
- winner = ' ';
- for (int i = 0; i < 3; i++)
- {
- // By rows
- if (board.Cells[i, 0] != '-' && board.Cells[i, 0] == board.Cells[i, 1] && board.Cells[i, 0] == board.Cells[i, 2])
- {
- winner = board.Cells[i, 0];
- return true;
- }
- // By columns
- if (board.Cells[0, i] != '-' && board.Cells[0, i] == board.Cells[1, i] && board.Cells[0, i] == board.Cells[2, i])
- {
- winner = board.Cells[0, i];
- return true;
- }
- }
- // By diags
- if (board.Cells[0, 0] != '-' && board.Cells[0, 0] == board.Cells[1, 1] && board.Cells[0, 0] == board.Cells[2, 2])
- {
- winner = board.Cells[0, 0];
- return true;
- }
- if (board.Cells[0, 2] != '-' && board.Cells[0, 2] == board.Cells[1, 1] && board.Cells[0, 2] == board.Cells[2, 0])
- {
- winner = board.Cells[0, 2];
- return true;
- }
- return result;
- }
- private static char SwitchPlayerOnTurn(char playerOnTurn)
- {
- if (playerOnTurn == 'X')
- {
- playerOnTurn = 'O';
- }
- else
- {
- playerOnTurn = 'X';
- }
- return playerOnTurn;
- }
- private static void PrintResult(Dictionary<string, int> resultCounts)
- {
- Console.WriteLine(resultCounts["X"]);
- Console.WriteLine(resultCounts["Draw"]);
- Console.WriteLine(resultCounts["O"]);
- }
- private static void PrintBoard(Board board, Dictionary<string, int> resultCounts)
- {
- for (int r = 0; r < 3; r++)
- {
- for (int c = 0; c < 3; c++)
- {
- Console.Write(board.Cells[r, c]);
- }
- Console.WriteLine();
- }
- foreach (var entry in resultCounts)
- {
- Console.WriteLine("{0} {1}", entry.Key, entry.Value);
- }
- Console.WriteLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement