Ay

Ay

  • NA
  • 1
  • 0

help with my first game please

Dec 17 2006 9:29 AM
Hello, Im writing my first ever game in C#. At the moment it takes input from the user and displays the grid with the correct location of the user input. For some reason it wont allow the computer to input a random number. my code is: using System; using System.Collections.Generic; using System.Text; namespace tictactoe { class Program { static char[,] matrix = new char[3, 3]; static char done; //ClearConsole ClearMyConsole = new ClearConsole(); static void Main(string[] args) { // ________________MAIN FUNCTION ______________________ screen_one(); Console.Clear(); initialise_matrix(); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Start DO LOOP do { display_matrix(); get_player_move(); check_emptycell(); done = check_emptycell(); if (done == 'd') { display_matrix(); break; } else { check_down(); done = check_down(); } if (done != ' ') { break; } else { display_matrix(); get_computers_move(); check_down(); done = check_down(); } if (done !=' '){ break;} else { display_matrix(); //CALL display_matrix function } display_matrix(); Console.Clear(); } while (done == ' '); //~~~~~~~~~~END DO LOOP if (done == 'O'){ display_matrix(); }//END IF if (done == 'd'){ //IF done = 'd' THEN Console.WriteLine("Its a draw!!!"); } else { Console.WriteLine("The winner is {0}",done); //OUTPUT “The winner is {0}",done //INPUT Console.ReadLine(); } }//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ END OF MAIN FUNCTION static char check_emptycell(){ //FOR i=0 to < 3 for(int i=0;i<3;i++){ if(matrix[i,0] == ' ') { return ' '; } else if (matrix[i,1] ==' '){ return ' '; } else if (matrix[i,1] == ' '){ return ' '; } //END IF } return 'd'; }// end check_emptycell function static void screen_one(){ string myvalue; do { Console.Clear(); Console.WriteLine("This Os and Xs game You to play the computer"); Console.WriteLine("press a key to start game"); myvalue = Console.ReadLine(); } while (myvalue != "");//END WHILE LOOP myval != "" } static void initialise_matrix(){ for (int i=0;i<3;i++){ for (int j = 0; j < 3; j++) matrix[i, j] =' '; } }//end initialise_matrix function static void display_matrix(){ for (int t = 0; t < 3;t++ ){ Console.WriteLine(" {0} | {1} | {2} ", matrix[t, 0], matrix[t, 1], matrix[t, 2]); if (t != 2) Console.WriteLine("\n---|---|---\n"); } Console.WriteLine(""); } static void get_player_move(){ int x,y; Console.WriteLine("Enter row for your X:"); x = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("enter column for your X:"); y = Convert.ToInt32(Console.ReadLine()); x=x-1; y=y-1; matrix[x, y] = 'X'; }//end get_player_move function //start check_down function static char check_down() { //int i=0; //FOR x=0 to < 3 for(int i=0;i<3;i++) if (matrix[0,i]==matrix[1,i] & matrix[0,i]==matrix[2,i]){ return matrix[0,i]; } else if (matrix[i,0]==matrix[i,1] & matrix[i,0]==matrix[i,2]){ return matrix[i,0]; }return ' '; //ELSEIF matrix[i,0]=matrix[i,1]AND matrix[i,0]=matrix[i,2]THEN } //START GET_COMPUTERS_MOVE static void get_computers_move(){ Random rand = new Random(); int x = rand.Next(4); int y = rand.Next(4); for(x=0;x<3;x++){ for(y=0;y<2;y++) if (matrix[x, y] == ' ') break; if (matrix[x, y] == ' ') break; if (x * y > 4) done='d'; else matrix[x,y] = 'O'; } }//end get_computers_move function } } thanks

Answers (1)