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 ;)
#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;
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()
{
Beep(784,500);
Beep(738,500);
Beep(684,500);
Beep(644,1000);
}
void Pesmica()
{
Beep(523,200);
Sleep(1);
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);
}
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)";
}
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");
}
}
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;
}
void Zvezdice()
{
cout<<"\n* * * * * *"
<<" * * * * * *"
<<" * * * * * *"
<<" * * * * * * * * * *"
<<"* * * ** * * *\n\n";
}
void Zmaga(int pNumb=0)
{
switch(pNumb)
{
case 1:
cout<<ime1<<" WINS!!!!!! Congrats!\n";
Zvezdice();
Pesmica();
break;
case 2:
cout<<ime2<<" WINS!!!!!! Congrats!\n";
Zvezdice();
Pesmica();
break;
}
}
int PreveriZmag()
{
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;
}
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;
}
if(A=='X'&&E=='X'&&I=='X')
{
Player1win=true;
}
if(G=='X'&&E=='X'&&C=='X')
{
Player1win=true;
}
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;
}
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;
}
if(A=='O'&&E=='O'&&I=='O')
{
Player2win=true;
}
if(G=='O'&&E=='O'&&C=='O')
{
Player2win=true;
}
if (Player1win==true)
{
system("CLS");
Zmaga(1);
return 1;
}
else
{
if (Player2win==true)
{
system("CLS");
Zmaga(2);
return 1;
}
}
}
void Napaka()
{
system("CLS");
cout<<"Oops, there's allready an X or O.\nTry again!";
Sleep(2000);
system("CLS");
Izrisi();
}
void Igralec1()
{
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();
}
void Igralec2()
{
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:
system("CLS");
cout<<"Oops, wrong letter. Try again!\n";
Sleep(2000);
Izrisi();
goto start;
break;
}
Izrisi();
}
int main()
{
int celZas;
cout<<"Start in full screen?\n1.Yes\n2.No\n>>";
cin>>celZas;
if(celZas==1)
{
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';
B='B';
C='C';
D='D';
E='E';
F='F';
G='G';
H='H';
I='I';
Izrisi();
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;
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;
KonecIgre();
end:
system("PAUSE");
}