using System; namespace PS4_Q1 { // // Probem Set 4, Question 1 // Written by Paul Hudak // October 7, 2006 // class Class1 { // // A program to play Tic Tac Toe // [STAThread] static void Main(string[] args) { // ask user for names of players Console.Write("Please type the name of the X player: "); string Xname = Console.ReadLine(); Console.Write("Please type the name of the O player: "); string Oname = Console.ReadLine(); // create new board and player objects Board bd = new Board(); Player Xp = new Player(Xname, 'X'); Player Op = new Player(Oname, 'O'); // initialize variables for loop int cnt = 1; // counts how many moves have been made bool Xturn = true; // X moves first char winner = '-'; // no winner yet // as long as the board still has empty squares, // and there is no winner, we keep playing, while // alternating moves between player X and player O. while (cnt<=9 && winner=='-') { if (Xturn) Xp.Move(bd); // X moves else Op.Move(bd); // O moves bd.Display(); // display board // update loop variables Xturn = !Xturn; cnt = cnt+1; winner = bd.Winner(); } // declare winner or draw string winName; if (winner=='X') Console.WriteLine("\nThe winner is {0}.\n", Xp.Name()); else if (winner=='O') Console.WriteLine("\nThe winner is {0}.\n", Op.Name()); else Console.WriteLine("\nThe game ended in a draw.\n"); } } class Board { // INSTANCE VARIABLES // create one variable for each square // top middle bottom; left middle right private char tl = '-'; private char tm = '-'; private char tr = '-'; private char ml = '-'; private char mm = '-'; private char mr = '-'; private char bl = '-'; private char bm = '-'; private char br = '-'; // CONSTRUCTORS // new board contains all blanks public Board() { } // INSTANCE METHODS // display board as 3x3 square of characters public void Display() { Console.Write("\n{0}{1}{2}\n{3}{4}{5}\n{6}{7}{8}\n", tl,tm,tr,ml,mm,mr,bl,bm,br); } // returns content of given square public char GetSquare(int x, int y) { // compute unique index for each square int i = 3*x + y; switch (i) { // i identifies the square uniquely case 0: return tl; case 1: return tm; case 2: return tr; case 3: return ml; case 4: return mm; case 5: return mr; case 6: return bl; case 7: return bm; case 8: return br; default: return 'Z'; // this should never happen } } // sets a square to given content public void SetSquare(char c, int x, int y) { // compute unique index for each square int i = 3*x + y; switch (i) { // i identifies the square uniquely case 0: tl=c; break; case 1: tm=c; break; case 2: tr=c; break; case 3: ml=c; break; case 4: mm=c; break; case 5: mr=c; break; case 6: bl=c; break; case 7: bm=c; break; case 8: br=c; break; default: break; // this should never happen } } // determine winner, if there is one public char Winner() { // most of the work is done in win if (win('X')) return 'X'; else if (win('O')) return 'O'; else return '-'; } // private helper functions private bool win(char c) { // check all 8 possibilities for a winner // (3 horizontal, 3 vertical, and 2 diagonal) return ( line(tl,tm,tr,c) || line(ml,mm,mr,c) || line(bl,bm,br,c) || line(tl,ml,bl,c) || line(tm,mm,bm,c) || line(tr,mr,br,c) || line(tl,mm,br,c) || line(tr,mm,bl,c) ); } private bool line(char x, char y, char z, char c) { // return true if we have a line of the same 3 characters return ( x==c && y==c && z==c ); } } class Player { // CLASS VARIABLES // static class variable shared by all Players private static Random randGen = new Random(); // INSTANCE VARIABLES private string name; private char role; // 'X', 'O', or '-' // CONSTRUCTORS public Player(string nm, char xoro) { // a Player's state is just name and role name = nm; role = xoro; } // INSTANCE METHODS // return name of player public string Name() { // return Player's name return name; } // make a random move on given board object public void Move(Board bd) { int x; int y; // generate a random board position that is not occupied do { // need two calls to the random number generator x = randGen.Next(3); y = randGen.Next(3); } while (bd.GetSquare(x,y) != '-'); // modify the board bd.SetSquare(role, x, y); } } }