Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleAppTicTacToe
- {
- public class TicTacToe
- {
- // Private class members
- private char[,] board = new char[3, 3];
- private int currentPlayer;
- private const int CoordinateLimit = 2;
- private const int playerX = 0; // todo check ENUM
- private const int playerO = 1;
- private readonly char[] marks = new char[2] {'x', 'o'};
- public TicTacToe()
- {
- Console.WriteLine("Game started!");
- currentPlayer = playerX;
- }
- // Public class functions
- public void displayBoard()
- {
- for(int y = 0; y < board.GetLength(1); y++)
- {
- for(int x = 0; x < board.GetLength(0); x++)
- {
- if ( board[x,y] == 0) // is empty
- {
- Console.Write("- ");
- }
- else
- {
- Console.Write(board[x,y]);
- Console.Write(' ');
- }
- }
- Console.WriteLine();
- }
- }
- public int currentPlayerInt()
- {
- return currentPlayer;
- }
- public string currentPlayerText()
- {
- return (currentPlayer == playerX) ? "Player X" : "Player O";
- }
- /*
- */
- public bool consoleSelectBox()
- {
- Console.WriteLine("Select box (x,y): ");
- Console.Write("x: ");
- int x = Int32.Parse(Console.ReadLine());
- Console.Write("y: ");
- int y = Int32.Parse(Console.ReadLine());
- return selectBox(x, y);
- }
- /* Selects box by current player
- Returns true if selected box is valid
- Returns false if box is already marked
- Returns false if x or y out of range
- */
- public bool selectBox(int x, int y)
- {
- // Check if x,y are in range
- if ( (x > CoordinateLimit) || (x < 0) || y > CoordinateLimit )
- {
- Console.WriteLine("Selected index out of range ");
- return false;
- }
- // Check if box is empty
- if ( board[x,y] != 0 )
- {
- Console.WriteLine("This box is already marked!");
- return false;
- }
- else
- {
- board[x,y] = marks[currentPlayer];
- checkWinner();
- // currentPlayer = !currentPlayer;
- if (currentPlayer == playerX)
- {
- currentPlayer = playerO;
- }
- else
- {
- currentPlayer = playerX;
- }
- Console.WriteLine(currentPlayer);
- this.displayBoard();
- return true;
- }
- }
- private bool checkWinner()
- {
- for (int col = 0; col < 3; col++)
- {
- }
- return false;
- }
- }
- public class Program
- {
- public static void Main()
- {
- TicTacToe game = new TicTacToe();
- game.displayBoard();
- Console.WriteLine(game.currentPlayerInt());
- Console.WriteLine(game.currentPlayerText());
- game.selectBox(1,1);
- Console.WriteLine(game.currentPlayerText());
- game.selectBox(1,2);
- Console.WriteLine();
- game.selectBox(1,1); //
- game.consoleSelectBox();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment