Thread: tic tac toe in Dev-C++ 4.9.9.2

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    tic tac toe in Dev-C++ 4.9.9.2

    Hi I wrote a tic tac toe game that uses 2 human players. However, I want to re-write the game where it only need 1 human and the second player is the computer. The only problem is I don't know how to do that. If someone has some good tips, link, or anything that can help me would great. Thanks.

    Here is my two player version
    Code:
    #include "simpio.h"
    #include <stdio.h>
    
    char matrix[3][3];//={0};
    
    void board(void);
    int main()
    {
    int m,n;
    char ch='y';
    char x='X';
    char o='O';
    char again;
    while(ch=='Y'||ch=='y'){
    for (m=0;m<3;m++)for (n=0;n<3;n++)matrix[m][n]= '\0';
    int i,j,sum=0;
    while ( sum < 10){
    if (sum == 0) board();
    printf("Player 1 is 'X': choose the row and column");
    printf("\nRow : ");
    i=GetInteger();
    printf("\nColumn : ");
    j=GetInteger();
    for (; i>3 || i<1 || j>3 || j<1 ||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1]);) 
    {
        printf("Sorry, but you gotta choose another place.\n");
        printf("row : ");
        i=GetInteger();
        printf("column : ");
        j=GetInteger();
        }
    matrix[i-1][j-1]='X';
    sum++;
    board();
    
    //check if wins
    if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) {printf("Player 1 wins");break;}
    if (matrix[2][0]=='X' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]) {printf("Player 1 wins");break;}
    if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]) {printf("Player 1 wins");break;}
    if (matrix[0][1]=='X' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]) {printf("Player 1 wins");break;}
    if (matrix[0][2]=='X' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]) {printf("Player 1 wins");break;}
    if (matrix[0][0]=='X' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]) {printf("Player 1 wins");break;}
    if (matrix[1][0]=='X' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]) {printf("Player 1 wins");break;}
    if (matrix[2][0]=='X' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]) {printf("Player 1 wins");break;}
    
    if (sum == 9){printf("The game is over and no one wins"); break;} //sum=9 because there are only 9 boxes in the game
    //player 2's turn
    
    printf("Player 2 is 'O': choose the row and column");
    printf("Row : ");
    i=GetInteger();
    printf("Column : ");
    j=GetInteger();
    for (;i>3 || i<1 || j>3 || j<1 ||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1]);) 
    {
    printf("Sorry, but you gotta choose another place.\n");printf("row : ");i=GetInteger();printf("column : ");j=GetInteger();}
    matrix[i-1][j-1]=o;
    sum++;
    //the play box
    board();
    //check if wins
    if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) {printf("Player 2 wins");break;}
    if (matrix[2][0]=='O' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]) {printf("Player 2 wins");break;}
    if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]) {printf("Player 2 wins");break;}
    if (matrix[0][1]=='O' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]) {printf("Player 2 wins");break;}
    if (matrix[0][2]=='O' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]) {printf("Player 2 wins");break;}
    if (matrix[0][0]=='O' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]) {printf("Player 2 wins");break;}
    if (matrix[1][0]=='O' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]) {printf("Player 2 wins");break;}
    if (matrix[2][0]=='O' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]) {printf("Player 2 wins");break;}
    
    }
    printf("\nWould you like to play again??? (Y - N)\n");
    ch=getchar();
    }
          getchar();
          return 0;
    }
    void board(void)
    {
    //the play box
    printf("\n\t\t                1   2   3\n");
    printf("\t\t             1  %c | %c | %c\n", matrix[0][0],matrix[0][1],matrix[0][2]); 
    printf("\t\t               ---|---|---\n"); 
    printf("\t\t             2  %c | %c | %c\n", matrix[1][0],matrix[1][1],matrix[1][2]);
    printf("\t\t               ---|---|---\n");         
    printf("\t\t             3  %c | %c | %c\n", matrix[2][0],matrix[2][1],matrix[2][2]);
    }

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    Any help would be greatly appreciated.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You will probably find this thread interesting. tic-tac-toe computer
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    106
    bascially a bunch of if statements with certain moves of greater priority then others... I did this a couple of months ago but used 0, 1, and 2; then took a nested for loop to dislay all the stuff to the screen... and had another for loop controlling checking for winners and before i had done it this way i just had a bunch of if statements... about 800 lines of code with the for loops it but it down to 100; I may have got of topic with this but I'm saying to basically plan before you program and espicially AI..... ALso check out the article noted about... good luck

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Before you move on, you need to fix that indentation. Very important.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    20
    The min-max algorithm can be used for this. Research min-max algorithm, and I think once you understand the algorithm, it is easy to program.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    106
    yeah min-max would work i thought about telling you that today... and came back to tell you... but another thing i noticed about your code is you variable names... when somebody looks at the code they dont know what m and n do... give them more exact names.. It may be a little longer but it will be easier to understand whats going on

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tic tac toe check winner
    By dhardin in forum C++ Programming
    Replies: 15
    Last Post: 12-20-2009, 07:57 PM
  2. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  3. Tic Tac Toe program
    By muzihc in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 08:08 PM
  4. Tic Tac Toe AI help please...
    By Rune Hunter in forum Game Programming
    Replies: 12
    Last Post: 11-05-2004, 04:24 PM
  5. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM