Can anyone write a tic tac toe in C.

2 replies
Hiya.

I want someone to write a tic tac toe in C, If you are able to do it inbox meh and I will provide you with the requirements and I will pay ofc.

Thanks. ;o
#tac #tic #toe #write
  • Profile picture of the author nthmarketing
    Here you go

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(){
       //This array of chars represents the game board, and it holds the content of each square (x,o or empty)
       char square[3][3] = { { ' ',' ',' '},{' ',' ',' '},{' ',' ',' ' } };
       //The current player (x or o)
       char turn = 'X';
       //These variables will hold the number of the row and column selected by the players.
       int row;
       int column;
       //The winner (x, o, t(if it`s a tie) or empty)
       char winner = ' ';
       //Required variables to check if a move is valid or not
       bool validmove;
       bool validrow;
       bool validcolumn;
       //The game loop`s again and again until the game is over
       while(winner == ' '){
          //Draw the board
          cout << " 1 2 3" << endl << " +---+---+---+" << endl;
          for(int i=1;i<4;i++){
             cout << " " << i << " | " <<  square[i-1][0] << " | " << square[i-1][1] << " | "  << square[i-1][2] << " |" << endl << "  +---+---+---+" << endl;
          }
          //Check for an eventual winner of for a tie
          for(int i=0;i<3;i++){
             if(square[i][0] == square[i][1] && square[i][1] == square[i][2] && square[i][0] != ' '){
                winner = square[i][0];
             }
          }
          for(int i=0;i<3;i++){
             if(square[0][i] == square[1][i] && square[1][i] == square[2][i] && square[0][i] != ' '){
                winner = square[0][i];
             }
          }
          if(square[0][0] == square[1][1] && square[1][1] == square[2][2] && square[0][0] != ' '){
             winner = square[0][0];
          }
          if(square[0][2] == square[1][1] && square[1][1] == square[2][0] && square[0][2] != ' '){
             winner = square[0][2];
          }
          if(square[0][0] == square[0][1] && square[0][1] ==  square[0][2] && square[0][2] == square[1][0] &&  square[1][0] == square[1][1] && square[1][1] == square[1][2]  && square[1][2] == square[2][0] && square[2][0] ==  square[2][1] && square[2][1] == square[2][2] &&  square[0][0] != ' '){
             winner = 't';
          }
          //If somebody won
          if(winner == 'X' || winner == 'O'){
             //Display congratulations message
             cout << "Congratulations! Player ";
             if(winner == 'X'){
                cout << 1;
             }
             else{
                cout << 2;
             }
             cout << " is the winner!" << endl;
             break;
          }
          else if(winner == 't'){
             //Display a message if it`s tie
             cout << "Tie!" << endl;
             break;
          }
          //If the game is not yet over show who`s the next player to move
          cout << "Player ";
          if(turn == 'X'){
             cout << 1;
          }
          else{
             cout << 2;
          } 
          cout << "'s turn:" << endl;
          //Loop until the player selects a valid square
          validmove = false;
          while(!validmove){
             validrow = false;
             //Loop until the player selects a valid row
             while(!validrow){
                cout << "Row: ";
                cin >> row;
                if(row == 1 || row == 2 || row == 3){
                   validrow = true;
                }
                else{
                   cout << endl << "Invalid row!" << endl;
                }
             }
             validcolumn = false;
             //Loop until the player selects a valid column
             while(!validcolumn){
                cout << "Column: ";
                cin >> column;
                if(column == 1 || column == 2 || column == 3){
                   validcolumn = true;
                }
                else{
                   cout << endl << "Invalid column!" << endl;
                }
             }
             //Change the turn to the next player
             if(square[row-1][column-1] == ' '){
                square[row-1][column-1] = turn;
                validmove = true;
                if(turn == 'X'){
                   turn = 'O';
                }
                else{
                   turn = 'X';
                }
             }
             //If the selected square is occupied display a message and loop again
             else{
                cout << "The selected square is occupied!" << endl << "Select again:" << endl;
             }
          }
       }
       system("pause");
    }
    Hope that is what your looking for. A million ways to write that code here is one.
    {{ DiscussionBoard.errors[4405496].message }}
  • Profile picture of the author nthmarketing
    And here it is in PHP

    Code:
    <html>
    <head>
    <title>tic tac toe</title>
    
    <script>
    function create_tic_tac_toe_ai_array()
             {
             ai_array = new Array(9);
    
             for(var index = 0; index < ai_array.length; index++)
                {
                ai_array[index] = new Array(ai_array.length - index);
                for(var index2 = 0; index2 < ai_array[index].length; index2++)
                    ai_array[index][index2] = index2;
                }
    
             return(ai_array);
             }
    
    function randomise_ai_array(tic_tac_toe_ai_array)
             {
             for(var index = 0; index < tic_tac_toe_ai_array.length; index++)
                for(var index2 = 0; index2 < tic_tac_toe_ai_array[index].length; index2++)
                   {
                   temp = tic_tac_toe_ai_array[index][index2];
                   rand = parseInt(Math.random() * tic_tac_toe_ai_array[index].length);
                   tic_tac_toe_ai_array[index][index2] = tic_tac_toe_ai_array[index][rand];
                   tic_tac_toe_ai_array[index][rand] = temp;
                   }
             }
    
    function create_tic_tac_toe()
             {
             this.Letter = '';
             this.Play = -1;
             }
    
    function create_tic_tac_toe_array()
             {
             game_array = new Array(9);
    
             for(var index = 0; index < ai_array.length; index++)
                 game_array[index] = new create_tic_tac_toe();
    
             return(game_array);
             }
    
    function next_available_square(tic_tac_toe_array, square)
             {
             empty_squares = 0;
    
             for(var index = 0; empty_squares <= square; index++)
                {
                if(tic_tac_toe_array[index].Letter == '')
                  {
                  empty_squares++;
                  }
                }
    
             return(index - 1);
             }
    
    function ai_play(tic_tac_toe_array, tic_tac_toe_ai_array, turn_number, trial_number)
             {
             var index = turn_number + trial_number;
    
             var possible_player_wins = 0;
    
             var possible_outcomes = new Array(tic_tac_toe_ai_array[index].length);
    
             //Each increment in this loop is a possible move.
             for(var index2 = 0; index2 < tic_tac_toe_ai_array[index].length; index2++)
                {
                next_square = next_available_square(tic_tac_toe_array, tic_tac_toe_ai_array[index][index2]);
    
                trial_move(tic_tac_toe_array, next_square, next_letter(tic_tac_toe_array));
    
                possible_outcomes[index2] = 0;
    
                if(win(tic_tac_toe_array, Computer_Letter()) == true)
                  {
                  possible_outcomes[index2] = -1 * (trial_number + 1);
                  }
    
                if(win(tic_tac_toe_array, Player_Letter()) == true)
                  {
                  possible_outcomes[index2] = (trial_number * 2) - possible_player_wins;
                  possible_player_wins++;
                  }
    
                if(trial_number < 3 && turn_number + trial_number < 8 && possible_player_wins == 0)
                  {
                  var return_value = ai_play(tic_tac_toe_array, tic_tac_toe_ai_array, turn_number, trial_number + 1);
    
                  if(possible_outcomes[index2] == 0)
                     possible_outcomes[index2] = return_value;
    
                  if(return_value == -1)
                     possible_outcomes[index2] = return_value;
    
                  if(possible_outcomes[index2] < -1 &&
                     return_value > 0)
                     possible_outcomes[index2] = return_value;
    
                  take_back_moves(tic_tac_toe_array, turn_number + trial_number + 1);
                  }
    
                take_back_moves(tic_tac_toe_array, turn_number + trial_number);
                }
    
             take_back_moves(tic_tac_toe_array, turn_number + trial_number);
    
             var best_choice = 0;
    
             if(trial_number > 0)
             for(var index2 = 0; index2 < possible_outcomes.length; index2++)
                {
                if(possible_outcomes[best_choice] == 0)
                   best_choice = index2;
    
                //Choose the closest possible player victory, to avoid it.
                if(possible_outcomes[best_choice] > 0 &&
                   possible_outcomes[index2] > 0 &&
                   possible_outcomes[best_choice] > possible_outcomes[index2])
                   best_choice = index2;
    
                //Choose possible player victories over possible computer victories, to avoid them.
                if(possible_outcomes[best_choice] < 0 &&
                   possible_outcomes[index2] > 0)
                   best_choice = index2;
                }
             else
             for(var index2 = 0; index2 < possible_outcomes.length; index2++)
                {
                //Dont let the player win.
                if(possible_outcomes[best_choice] > 0 &&
                   possible_outcomes[index2] <= 0)
                   best_choice = index2;
    
                //Mitigate the players possible wins.
                if(possible_outcomes[best_choice] > 0 &&
                   possible_outcomes[index2] > possible_outcomes[best_choice])
                   best_choice = index2;
    
                //Let the computer win.
                if(possible_outcomes[index2] == -1)
                   best_choice = index2;
                }
    
             if(trial_number == 0)
               {
               next_square = next_available_square(tic_tac_toe_array, tic_tac_toe_ai_array[index][best_choice]);
               move(tic_tac_toe_array, next_square, next_letter(tic_tac_toe_array));
               }
    
             take_back_moves(tic_tac_toe_array, turn_number + trial_number + 1);
             return(possible_outcomes[best_choice]);
             }
    
    function Computer_Letter()
             {
             return(computer_letter);
             }
    
    function Player_Letter()
             {
             return(player_letter);
             }
    
    function play()
             {
             if(turn == Computer_Letter())
                document.tic_tac_toe.message.value = 'My turn (' + Computer_Letter() + ')';
             else
                document.tic_tac_toe.message.value = 'Your turn (' + Player_Letter() + ')';
    
             if(win(tic_tac_toe, Computer_Letter()) == false &&
                win(tic_tac_toe, Player_Letter()) == false &&
                tied(tic_tac_toe) == false)
               {
               if(turn == Computer_Letter())
                 {
                 ai_play(tic_tac_toe, ai_array, move_number(tic_tac_toe), 0);
                 turn = Player_Letter();
                 }
    
               setTimeout('play();', 1000);
               }
    
             if(win(tic_tac_toe, Computer_Letter()))
               {
               document.tic_tac_toe.message.value = 'I win';
               }
    
             if(win(tic_tac_toe, Player_Letter()))
               {
               document.tic_tac_toe.message.value = 'you win';
               }
    
             if(tied(tic_tac_toe))
               {
               document.tic_tac_toe.message.value = 'we tied';
               }
             }
    
    function move_number(tic_tac_toe_array)
             {
             latest_move = 0;
             for(var index = 0; index < tic_tac_toe_array.length; index++)
                {
                if(tic_tac_toe_array[index].Play >= latest_move)
                   latest_move = tic_tac_toe_array[index].Play + 1;
                }
    
             return(latest_move);
             }
    
    function next_letter(tic_tac_toe_array)
             {
             last_position = 0;
             for(var index = 0; index < tic_tac_toe_array.length; index++)
                {
                if(tic_tac_toe_array[index].Play > tic_tac_toe_array[last_position].Play)
                   last_position = index;
                }
    
             if(tic_tac_toe_array[last_position].Letter == Computer_Letter())
                return(Player_Letter());
             else
                return(Computer_Letter());
             }
    
    function user_move(square, Letter)
             {
             if(tic_tac_toe[square].Letter == '' && turn == Player_Letter())
               {
               move(tic_tac_toe, square, Letter);
               turn = Computer_Letter();
               document.tic_tac_toe.message.value = 'My turn (' + Computer_Letter() + ')';
               }
             }
    
    function move(tic_tac_toe_array, square, Letter)
             {
             tic_tac_toe_array[square].Letter = Letter;
             tic_tac_toe_array[square].Play = move_number(tic_tac_toe_array);
    
             eval('document.tic_tac_toe.square' + square + '.value = \'' + Letter + '\'');
             }
    
    function trial_move(tic_tac_toe_array, square, Letter)
             {
             if(square < tic_tac_toe_array.length)
               {
               tic_tac_toe_array[square].Letter = Letter;
               tic_tac_toe_array[square].Play = move_number(tic_tac_toe_array);
    
               eval('document.tic_tac_toe.square' + square + '.value = \'' + Letter + '\'');
               }
             }
    
    function take_back_moves(tic_tac_toe_array, Play)
             {
             for(var index = 0; index < tic_tac_toe_array.length; index++)
                {
                if(tic_tac_toe_array[index].Play >= Play)
                  {
                  tic_tac_toe_array[index].Letter = '';
                  tic_tac_toe_array[index].Play = -1;
                  eval('document.tic_tac_toe.square' + index + '.value = \'\'');
                  }
                }
             }
    
    var ai_array;
    var tic_tac_toe;
    var turn;
    var computer_letter;
    var player_letter;
    
    function new_game()
             {
             if(Math.random() < 0.5)
               {
               computer_letter = 'x';
               player_letter = 'o';
               }
             else
               {
               computer_letter = 'o';
               player_letter = 'x';
               }
    
             if(Math.random() < 0.5)
                turn = Computer_Letter();
             else
                turn = Player_Letter();
    
             randomise_ai_array(ai_array);
    
             take_back_moves(tic_tac_toe, 0);
             }
    
    function init()
             {
             ai_array = create_tic_tac_toe_ai_array();
             tic_tac_toe = create_tic_tac_toe_array();
    
             new_game();
    
             play();
             }
    
    function win(tic_tac_toe_array, Letter)
             {
             //First row, first column, and diagonal.
             if(tic_tac_toe_array[0].Letter == Letter)
               {
               if(tic_tac_toe_array[1].Letter == Letter && tic_tac_toe_array[2].Letter == Letter)
                  return(true);
               if(tic_tac_toe_array[3].Letter == Letter && tic_tac_toe_array[6].Letter == Letter)
                  return(true);
               if(tic_tac_toe_array[4].Letter == Letter && tic_tac_toe_array[8].Letter == Letter)
                  return(true);
               }
    
             //Second row, second column, and other diagonal.
             if(tic_tac_toe_array[4].Letter == Letter)
               {
               if(tic_tac_toe_array[3].Letter == Letter && tic_tac_toe_array[5].Letter == Letter)
                  return(true);
               if(tic_tac_toe_array[1].Letter == Letter && tic_tac_toe_array[7].Letter == Letter)
                  return(true);
               if(tic_tac_toe_array[2].Letter == Letter && tic_tac_toe_array[6].Letter == Letter)
                  return(true);
               }
    
             //Third row and third column.
             if(tic_tac_toe_array[8].Letter == Letter)
               {
               if(tic_tac_toe_array[6].Letter == Letter && tic_tac_toe_array[7].Letter == Letter)
                  return(true);
               if(tic_tac_toe_array[2].Letter == Letter && tic_tac_toe_array[5].Letter == Letter)
                  return(true);
               }
    
             return(false);
             }
    
    function tied(tic_tac_toe_array)
             {
             if(win(tic_tac_toe_array, Player_Letter()) == false &&
                win(tic_tac_toe_array, Computer_Letter()) == false)
               {
                for(var index = 0; index < tic_tac_toe_array.length; index++)
                    if(tic_tac_toe_array[index].Letter == '')
                       return(false);
    
               return(true);
               }
    
             return(false);
             }
    </script>
    </head>
    
    <body onload="init();">
    
    <form name="tic_tac_toe">
    <table>
    <tr>
    <td colspan="3" align="center">
    <input type="text" size="9" name="message" style="border:none; text-align:center;" /></td>
    </tr>
    
    <tr>
    <td>
    <input type="text" size="5" name="square0" readonly
           onclick="user_move(0, Player_Letter());" /></td>
    <td align="center">
    <input type="text" size="5" name="square1" readonly
           onclick="user_move(1, Player_Letter());" /></td>
    <td align="right">
    <input type="text" size="5" name="square2" readonly
           onclick="user_move(2, Player_Letter());" /></td>
    </tr>
    
    <tr>
    <td>
    <input type="text" size="5" name="square3" readonly
           onclick="user_move(3, Player_Letter());" /></td>
    <td align="center">
    <input type="text" size="5" name="square4" readonly
           onclick="user_move(4, Player_Letter());" /></td>
    <td align="right">
    <input type="text" size="5" name="square5" readonly
           onclick="user_move(5, Player_Letter());" /></td>
    </tr>
    
    <tr>
    <td>
    <input type="text" size="5" name="square6" readonly
           onclick="user_move(6, Player_Letter());" /></td>
    <td align="center">
    <input type="text" size="5" name="square7" readonly
           onclick="user_move(7, Player_Letter());" /></td>
    <td align="right">
    <input type="text" size="5" name="square8" readonly
           onclick="user_move(8, Player_Letter());"  /></td>
    </tr>
    
    <tr>
    <td colspan="3"><input type="button" value="new game"
                           onclick="new_game();
                                    play();" /></td>
    </tr>
    </table>
    
    <input type="checkbox" name="debug_switch" />
    </form>
    </body>
    </html>
    {{ DiscussionBoard.errors[4405504].message }}

Trending Topics