Results 1 to 10 of 10

Thread: my pretty much final tic tac toe game...

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2001
    Posts
    7

    my pretty much final tic tac toe game...

    here it is:


    #include <iostream.h>
    #include <ctype.h>
    #include <fstream.h>
    #include "board class.h"
    #include "board functions.h"

    //oops, no comments are included in this... my mistake!

    int main(){

    char quit='Y';
    int turn(1);


    cout<<"\t\t***************************"<<endl
    <<"\t\t******* Welcome to ********"<<endl
    <<"\t\t***** Cybernetic2024's ****"<<endl
    <<"\t\t** (Russell Elfenbein's) **"<<endl
    <<"\t\t*** Naughts and Crosses ***"<<endl
    <<"\t\t***************************"<<endl<<endl<<endl;

    cout<<"What is your name, player 1? - ";
    cin.getline(player1,15);

    cout<<endl<<endl
    <<"What is your name, player 2? - ";
    cin.getline(player2,15);

    while(toupper(quit) != 'N'){

    game_board.alter_board();
    game_board.log();
    cout<<"\n\n\nWould you like to play again(Y/N)? ";
    cin>>quit;
    game_board.reset_board();
    }

    return 0;
    }

    ----------------------------------------

    //Tic Tac Toe Board Class!

    class Cboard{
    char board[8];
    int turn, wonyet;

    public:
    int print_board();
    int alter_board();
    int reset_board();
    int win();
    int log();
    }game_board;

    ------------------------------------------

    //Board Class functions
    char player1[15], player2[15];
    char * winner;
    int player1_wins(0), player2_wins(0);


    Cboard:rint_board(){ // prints the current instance of the board

    cout<<endl<<endl<<endl
    <<"\t\t1) "<<board[0]<<" | 2) "<<board[1]<<" | 3) "<<board[2]<<endl
    <<"\t\t--------------------- "<<endl
    <<"\t\t4) "<<board[3]<<" | 5) "<<board[4]<<" | 6) "<<board[5]<<endl
    <<"\t\t---------------------"<<endl
    <<"\t\t7) "<<board[6]<<" | 8) "<<board[7]<<" | 9) "<<board[8]<<endl<<endl<<endl;
    return 0;
    }

    //-------------------------------------------------------------------

    Cboard::alter_board(){ //changes the instance of the board.(add O's or X's, and pretty much game control
    int move, player, turn(1), count(0);
    char symbol;
    wonyet = 0;

    for(count=0;count<9;count++){

    player = turn%2;

    if(player==1){
    symbol = 'X';
    cout<<endl<<endl<<"where would you like to check "<<player1<<"? - ";
    game_board.print_board();
    }
    else{
    symbol = 'O';
    cout<<endl<<endl<<"where would you like to check "<<player2<<"? - ";
    game_board.print_board();
    }
    cin>>move;


    while(move<1||move>9||board[(move-1)]=='O'||board[(move-1)]=='X'){
    cout<<endl<<endl<<"\a### Error ### \nInput error, no such square! choose again - ";
    game_board.print_board();
    cin>>move;
    }

    board[(move-1)]=symbol;

    turn++;


    game_board.print_board();

    wonyet = game_board.win();
    if(wonyet == 1){
    count = 10;
    }

    }
    return 0;
    }


    //------------------------------------------------------------------

    Cboard::win(){
    char XorO;
    int r;

    for(int x=0;x<2;x++){
    if(x==0){
    XorO = 'X';
    }

    if (x==1){
    XorO = 'O';
    }


    if(board[0]==XorO && board[1]==XorO && board[2]==XorO||board[3]==XorO && board[4]==XorO && board[5]==XorO
    ||board[6]==XorO && board[7]==XorO && board[8]==XorO||board[0]==XorO && board[3]==XorO && board[6]==XorO||board[1]==XorO && board[4]==XorO && board[7]==XorO
    ||board[2]==XorO && board[5]==XorO && board[8]==XorO||board[0]==XorO && board[4]==XorO && board[8]==XorO||board[2]==XorO && board[4]==XorO && board[6]==XorO){
    if(XorO =='X'){ cout<<endl<<endl
    <<player1<<" wins!\a"<<endl<<endl;
    winner = player1;
    player1_wins++;
    }

    if(XorO =='O'){ cout<<endl<<endl
    <<player2<<" wins!\a"<<endl<<endl;
    winner = player2;
    player2_wins++;
    }


    r = 1;
    }
    }

    cout<<endl
    <<"\t\t"<<player1<<"'s wins = "<<player1_wins<<endl
    <<"\t\t"<<player2<<"'s wins = "<<player2_wins<<endl<<endl;
    return (r);
    }

    //--------------------------------------------------

    Cboard::log(){
    ofstream log_file("log.txt",ios::app);

    log_file<<"\t\t1) "<<board[0]<<" | 2) "<<board[1]<<" | 3) "<<board[2]<<endl
    <<"\t\t--------------------- "<<endl
    <<"\t\t4) "<<board[3]<<" | 5) "<<board[4]<<" | 6) "<<board[5]<<endl
    <<"\t\t---------------------"<<endl
    <<"\t\t7) "<<board[6]<<" | 8) "<<board[7]<<" | 9) "<<board[8]<<endl
    <<endl<<"The winner was "<<winner<<endl<<endl<<endl;

    log_file.close();
    return 0;
    }

    Cboard::reset_board(){
    for(int x=0;x<9;x++){
    board[x]=' ';
    }
    return 0;
    }

    ------------------------------------------

    and there you have it...

    comments?
    Search message boards with MessageKing

  2. #2
    PowerPoster Arbiter's Avatar
    Join Date
    Sep 2000
    Location
    Manchester
    Posts
    2,276
    Damnedest kind of VB I ever saw....
    Gentile or Jew,
    O you who turn the wheel and look to windward,
    Consider Phlebas, who was once handsome and tall as you...

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Damnedest kind of VB I ever saw....
    Maybe beacuse it is C++.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  5. #5
    PowerPoster Arbiter's Avatar
    Join Date
    Sep 2000
    Location
    Manchester
    Posts
    2,276
    Really?

    Oh wow! I hadn't realised!!
    Gentile or Jew,
    O you who turn the wheel and look to windward,
    Consider Phlebas, who was once handsome and tall as you...

  6. #6
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205

    TTT AI

    Hey, has anyone developed an 'A.I' for tic-tac-toe.

    I know it's simple, but most(ALL) TTT games I see work the computer move by running it through every board combo and making the appropriate move.

    I just made one where the computer is initially VERY easy to beat, but after many games, it will learn which moves lead it to win, and which lead it to lose, and thus I am hoping it will 'learn' to make its own decisions based on its game history.

    If anyone is interested, I wouldn't mind some testers...
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  7. #7
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Yeah go on post the code.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  8. #8
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205

    Here we go...

    Here you go.

    I am aware of some bugs, but haven't had a chance to sort them out...

    I just added another section to the computer's AI which I am not sure is working correctly, but we'll see...

    Be sure to set the difficulty to 'Hard' when you run the program, or the computer will make completely random moves.

    If you feel like fiddling with the DB, the password is 'ttt'.

    Cheers.
    Attached Files Attached Files
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  9. #9
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Hmmm crashed with an AccessDB error when I selected Hard and then took my move

    Probably because you'd hardcoded the path :

    VB Code:
    1. Const strConnection = "Provider=MSDASQL.1;Persist Security Info=False;Extended Properties='DBQ=C:\VB Projects\TTT AI\ttt.mdb;DefaultDir=C:\VB Projects\TTT AI;Driver={Microsoft Access Driver (*.mdb)};DriverId=25;FIL=MS Access;FILEDSN=C:\VB Projects\TTT AI\ttt.mdb.dsn;MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;PWD=ttt;UserCommitSync=no;';Initial Catalog=C:\VB Projects\TTT AI\ttt"

    Would I be right i saying that you're basically keeping account of how successful the computer player was given certain board configurations ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  10. #10
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Whoops... I just C&P the connection string...
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width