Skip to content

Instantly share code, notes, and snippets.

@cwake
Last active February 4, 2017 01:44
Show Gist options
  • Save cwake/a3d8ab5202d5a650f914aeb5bd5a9b0c to your computer and use it in GitHub Desktop.
Save cwake/a3d8ab5202d5a650f914aeb5bd5a9b0c to your computer and use it in GitHub Desktop.
using System;
namespace p1010
{
class TicTacToe { // setting up the TicTacToe class
static int [,] board; // initialize the board with array
string playerName; //set up player name
public TicTacToe(int player, string playerName){ //accept player
PlayerName = playerName;
Player= player;//initialize player
board = new int [3,3]; //initialize the board 3 by 3
}
public int Player{get; set}
public string PlayerName{
get{return this.playerName}
set{
if ( value.Length > 0) //verify the player name
playerName = value;
else{// ask again because the input was not long enough or nothing was entered
while(value.Length < 1)
{
Console.WriteLine("That name does not work! Please enter a valid name.");// prompt player to enter name again
value = Console.ReadLine();//check the new input again
}
}
}
}
public bool Game()
{
int i= 0;
int j= 0;
Random random1 = new Random();
Random random2 = new Random();
if(PlayerName != "computer") //check for human playerName
{
Console.WriteLine("What position spot would you like to play?");
int.TryParse(Console.ReadLine().Trim(), out i); //trim is to check for extra spaces entered by the user, eliminate whitespace
int.TryParse(Console.ReadLine().Trim(), out j);//read in the second location
}
else // the player is a computer
{
i= random1.Next(1,4);
for(int a = 0; a< 6000; ++a)//keep computer from looping forever
j= random2.Next(1,4);// random turns made by computer
}
while (gameBoard(i,j))
{
}
private bool gameBoard(int i, int j)
{
bool YES = false;
if(i>3 || j>3 || i<1 || j<1) // check if the coordinates entered are valid
return false; // if not then that move is false
if( boad[i-1, j-1] != 1 && board [i-1, j-1] !=2)// checking if the coordinates are already filled, array starts at 0 so we subract one to get true place
YES = true;
return YES;
}
}
public static void makeBoard()
{
for(int i = 0; i <3; ++i)
{
for ( int j =0; j<3; ++j)
board[i,j]= 0;
}
showBoard();
}
private static showBoard(){
for(int i =0; i<3; ++i)
{
for ( int j =0; j<3; ++j)
Console.Write(board[i,j] + " ");// print
Console.WriteLine();
}
Console.WriteLine(); // move down one line
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment