Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Tic Tac Toe - simplifyed!

0.00/5 (No votes)
23 Oct 2008 1  
The most simple version of the famous game, written in C++! Enjoy!
main_ENG

Introduction

This is a simple game that any beginner can write. I program in C++ for 3 months now, im 16 years old, and i was able to make it, so can you.

Background

The idea of this game is: player 1 and player 2 compete to score 3X or 3O in a row. first one to

score it, wins, if no-one scores, both loose.

Code uses only simple If and Switch sentences, so its really simple in structure.

Using the code

You can copy/paste the code below to your C++ compiler. I recommend DevC++, which i use myself. You can allso download the Main_eng.zip file, which contains the main.cpp file.

Have fun, good luck, and send me some comments to my mail ;)

/*
****************************************
**********KRIZCI IN KROZCI**************
******Tic Tac Toe-English version*******
**********(C) Matic Miklavcic***********
*******miklavcicmatic@gmail.com*********
*+*+*+*+*You are free to mod it*+*+*+*+*
****************************************
Programmed in DevC++, 10. OCT 2009 *****
*/
#include <cstdlib>//libraries
#include <iostream>
#include <string>
#include <windows.h>//-------
using namespace std;
char A,B,C;
char D,E,F;
char G,H,I;
bool Player1win,Player2win;
string ime1,ime2;
//Maximise the console, if player chooses so
BOOL NT_SetConsoleDisplayMode(HANDLE hOutputHandle, DWORD dwNewMode)
{
    typedef BOOL (WINAPI *SCDMProc_t) (HANDLE, DWORD, LPDWORD);
    SCDMProc_t SetConsoleDisplayMode;
    HMODULE hKernel32;
    BOOL bFreeLib = FALSE, ret;
    const char KERNEL32_NAME[] = "kernel32.dll";

    hKernel32 = GetModuleHandleA(KERNEL32_NAME);
    if (hKernel32 == NULL)
    {
        hKernel32 = LoadLibraryA(KERNEL32_NAME);
        if (hKernel32 == NULL)
            return FALSE;

        bFreeLib = true;
    }

    SetConsoleDisplayMode = 
        (SCDMProc_t)GetProcAddress(hKernel32, "SetConsoleDisplayMode");
    if (SetConsoleDisplayMode == NULL)
    {
        SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
        ret = FALSE;
    }
    else
    {
        DWORD dummy;
        ret = SetConsoleDisplayMode(hOutputHandle, dwNewMode, &dummy);
    }
        
    if (bFreeLib)
        FreeLibrary(hKernel32);

    return ret;
}
void KonecIgre()//play the sad melody :((
{
Beep(784,500);
Beep(738,500);
Beep(684,500);
Beep(644,1000);
}
void Pesmica()//play the victorious melody :))
{
Beep(523,200);//use Beep(frequency,time); to play a sound in internal speaker
Sleep(1);//stop the program for (time in ms)
Beep(523,200);
Sleep(1);
Beep(523,200);
Sleep(1); 
Beep(659,700);
Beep(784,500);
Beep(523,200);
Sleep(1);
Beep(523,200);
Sleep(1);
Beep(523,200);
Sleep(1); 
Beep(659,700);
Beep(784,500);
Sleep(800);
Beep(784,400);
Beep(884,200);
Beep(784,200);
Beep(687,200);
Beep(659,200);
Beep(579,200);
Beep(519,400);
}


//write out help
int Help()
{
    system("CLS");
    cout<<"Help\n-----\nEach player must enter letters from A-G, until someone is" +
    "victorious or all the fields are filled.\n-------\nBy Mickey\n(this screen" +
    "vanishes in 7 secs)";
}


//write out Menu
int Menu()
{ 
    zacetek:
    int izbira;
    cout<<"Hello!\n-----\n1.Start the game\n2.Help\n3.Exit\n\n>> ";
    cin>>izbira;
    switch(izbira)
    {
         case 1:
              goto point;
              break;
              case 2:
                   Help();
                   Sleep(7000);
                   system("CLS");
                   goto zacetek;
                   break;
                      case 3: 
                      exit(1); 
                      break;
              point:
                        system("CLS");
    }
}



//display the table
void Izrisi()
{
  system("CLS");
  A=toupper(A);
  B=toupper(B);
  C=toupper(C);
  D=toupper(D);
  E=toupper(E);
  F=toupper(F);
  G=toupper(G);
  H=toupper(H);
  I=toupper(I); 
  cout<<"|---|---|---|"<<endl
      <<"| "<<A<<" | "<<B<<" | "<<C<<" |"<<endl
      <<"|---|---|---|"<<endl
      <<"| "<<D<<" | "<<E<<" | "<<F<<" |"<<endl
      <<"|---|---|---|"<<endl
      <<"| "<<G<<" | "<<H<<" | "<<I<<" |"<<endl
      <<"|---|---|---|"<<endl<<endl;
} 
//display the stars when victorious    
void Zvezdice()
{
 cout<<"\n*    *    *    *    *    *"
     <<"  *    *    *    *    *    *"
     <<" *    *    *    *    *    *"
     <<" *  *    * *     * *  * *   *  *"
     <<"*   *      *     **   *   * *\n\n";    
 }


//write out the winner
void Zmaga(int pNumb=0)
{
     switch(pNumb)
     {
           case 1://if player 1 won
                cout<<ime1<<" WINS!!!!!! Congrats!\n";
                Zvezdice();
                Pesmica();
                break;
           case 2://if player 2 won
                cout<<ime2<<" WINS!!!!!! Congrats!\n"; 
                Zvezdice();
                Pesmica();
                break;      
     }
}





//check for winner
int PreveriZmag()
{
    //check all X
    //Horizontal
        if(A=='X'&&B=='X'&&C=='X')
    {
          Player1win=true;          
    }
        if(D=='X'&&E=='X'&&F=='X')
    {
           Player1win=true;         
    }
        if(G=='X'&&H=='X'&&I=='X')
    {
        Player1win=true;            
    }
    //Vertical
        if(A=='X'&&D=='X'&&G=='X')
    {
         Player1win=true;           
    }
        if(B=='X'&&E=='X'&&H=='X')
    {
          Player1win=true;          
    }
        if(C=='X'&&F=='X'&&I=='X')
    {
         Player1win=true;           
    }
    //X-wise
        if(A=='X'&&E=='X'&&I=='X')
    {
           Player1win=true;         
    }
        if(G=='X'&&E=='X'&&C=='X')
    {
          Player1win=true;          
    }
    /*  start of O-s
    check it all over again
    _-_-_-_-_-_-_-_-_-_-_-*/
    //Horisontal
     if(A=='O'&&B=='O'&&C=='O')
    {
          Player2win=true;          
    }
        if(D=='O'&&E=='O'&&F=='O')
    {
           Player2win=true;         
    }
        if(G=='O'&&H=='O'&&I=='O')
    {
        Player2win=true;            
    }
    //Vertical
        if(A=='O'&&D=='O'&&G=='O')
    {
         Player2win=true;           
    }
        if(B=='O'&&E=='O'&&H=='O')
    {
          Player2win=true;          
    }
        if(C=='O'&&F=='O'&&I=='O')
    {
         Player2win=true;           
    }
    //X-wise
        if(A=='O'&&E=='O'&&I=='O')
    {
           Player2win=true;         
    }
        if(G=='O'&&E=='O'&&C=='O')
    {
          Player2win=true;          
    }
    /*check both bool-s ->TRUE,
    if yes, continue, if no,
    cout the winner*/
    
    
    if (Player1win==true)
       {//player 1 wins
       system("CLS");
       Zmaga(1);
       return 1;
      
       
                      
       } 
       else
       {//if player 1 didnt won, check the player 2
           if (Player2win==true)//if not, continue the game
       {
       system("CLS");
       Zmaga(2);
      
          return 1;        
       } 
       }
    
    
    
}





//wrong X or O location, try again
void Napaka()
{
    system("CLS");
    cout<<"Oops, there's allready an X or O.\nTry again!";
    Sleep(2000);
    system("CLS");
    Izrisi();
}
//player1 - play!
void Igralec1()
{//main function of player 1!!!!
char choice;
start:
cout<<ime1<<" (X): ";
cin>>choice;
choice=toupper(choice);
switch(choice)
{
     case 'A':
     if(A!='O')A='X';
     else 
     {
          Napaka();
          goto start;
     }
     break;
     case 'B':
     if(B!='O')B='X';
     else 
     {
          Napaka();
          goto start;
     }
     break;
     case 'C':
     if(C!='O')C='X';
     else 
     {
          Napaka();
          goto start;
     }
     break;
     case 'D':
     if(D!='O')D='X';
     else 
     {
          Napaka();
          goto start;
     }
     break;
     case 'E':
     if(E!='O')E='X';
     else 
     {
          Napaka();
          goto start;
     }
     break;
     case 'F':
     if(F!='O')F='X';
     else 
     {
          Napaka();
          goto start;
     }
     break;
     case 'G':
     if(G!='O')G='X';
     else 
     {
          Napaka();
          goto start;
     }
     break;
     case 'H':
     if(H!='O')H='X';
     else 
     {
          Napaka();
          goto start;
     }
     break;
     case 'I':
     if(I!='O')I='X';
     else 
     {
          Napaka();
          goto start;
     }
     break;
     default:
     system("CLS");
     cout<<"Oops, wrong letter. Try again.\n";
     Sleep(2000);
     Izrisi();
        goto start;     
     break;
}  
Izrisi();     
}
//player 2- play!
void Igralec2()
{//main function of player 2!
char choice;
start:
cout<<ime2<<" (O): ";
cin>>choice;
choice=toupper(choice);
switch(choice)
{
     case 'A':
     if(A!='X')A='O';
     else 
     {
          Napaka();
          goto start;
     }
     break;
     case 'B':
     if(B!='X')B='O';
     else 
     {
          Napaka();
          goto start;
     }
     break;
     case 'C':
     if(C!='X')C='O';
     else 
     {
          Napaka();
          goto start;
     }
     break;
     case 'D':
     if(D!='X')D='O';
     else 
     {
          Napaka();
          goto start;
     }
     break;
     case 'E':
     if(E!='X')E='O';
     else 
     {
          Napaka();
          goto start;
     }
     break;
     case 'F':
     if(F!='X')F='O';
     else 
     {
          Napaka();
          goto start;
     }
     break;
     case 'G':
     if(G!='X')G='O';
     else 
     {
          Napaka();
          goto start;
     }
     break;
     case 'H':
     if(H!='X')H='O';
     else 
     {
          Napaka();
          goto start;
     }
     break;
     case 'I':
     if(I!='X')I='O';
     else 
     {
          Napaka();
          goto start;
     }
     break;
          default://if player 1 enters the wrong letter,warn him and try again
          system("CLS");
        cout<<"Oops, wrong letter. Try again!\n";
        Sleep(2000);
        Izrisi();
        goto start;     
     break;
} 
 Izrisi(); 
}









//Main function- combines all subfunctions!
int main()
{
    int celZas;
    cout<<"Start in full screen?\n1.Yes\n2.No\n>>";
    cin>>celZas;
      if(celZas==1)
      {//Full screen-yes
        NT_SetConsoleDisplayMode( GetStdHandle( STD_OUTPUT_HANDLE ), 1 );
      }
    system("CLS");
    cout<<"Tic Tac Toe\n";
    Menu();
    cout<<"Name of player 1: ";
    cin>>ime1;
    cout<<"Name of player 2: ";
    cin>>ime2;
int retPreveri;    
Player1win=false;
Player2win=false;
char choice;
A='A';//set all the variables to their default value
B='B';
C='C';
D='D';
E='E';
F='F';
G='G';
H='H';
I='I';
Izrisi();
Igralec1();//Game starts!!!!
retPreveri=PreveriZmag();
if(retPreveri==1)goto end;
Igralec2();
retPreveri=PreveriZmag();
if(retPreveri==1)goto end;
Igralec1();
retPreveri=PreveriZmag();
if(retPreveri==1)goto end;
Igralec2();
retPreveri=PreveriZmag();
if(retPreveri==1)goto end;
Igralec1();
retPreveri=PreveriZmag();
if(retPreveri==1)goto end;
Igralec2();
retPreveri=PreveriZmag();
if(retPreveri==1)goto end;
Igralec1();
retPreveri=PreveriZmag();
if(retPreveri==1)goto end;
Igralec2();
retPreveri=PreveriZmag();//------
if(retPreveri==1)goto end;
system("CLS");
cout<<ime1<<", "<<ime2<<", 
    game is over!!! No-one won! Better luck next time."<<endl<<endl;//if noone won
KonecIgre();//play the sad song, as no-one won :((
end://end
system("PAUSE");
}
/*
Here are the translations of some functions, as they were written in slovenian language
(don't feel like retyping the whole game ;)
ime->name
KonecIgre->end of the game
Igralec(1,2)->player
PreveriZmag->check for winner
Izrisi->draw
Zvezdice->stars
Napaka->Error
Zmaga->victory
-------------
Have fun with the game,
your Mickey ("/(^_^)
*/




















 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here