Skip to content

Instantly share code, notes, and snippets.

@ethanny2
Created May 14, 2014 02:41
Show Gist options
  • Save ethanny2/a27ce5ad16b33559b935 to your computer and use it in GitHub Desktop.
Save ethanny2/a27ce5ad16b33559b935 to your computer and use it in GitHub Desktop.
A sample Tic Tac Toe program I made without a GUI, when I started with C++
#include <iostream>
using namespace std;
int main()
{
const int ROWS=3;
const int COLUMNS=3;
char board[ROWS][COLUMNS]={{'1','2','3'},
{'4','5','6'},
{'7','8','9'}};
int X;
int O;
bool win;
cout<<"\tWelcome to Tic-tac Toe!\n";
cout<<"\n\nThis is a two player game that simulates the classic";
cout<<"\nTic tac toe game. Decided who is 'X' and who is '0'. X always goes first.\n";
cout<<"Enter the corresponding number to input your 'X' or '0'\n";
cout<<"\nThe board is now\n";
for(int i=0;i<ROWS;++i){
for(int j=0;j<COLUMNS;++j){
cout<<board[i][j];
}
cout<<endl;
}
do{
bool valid=false;
cout<<"\nPlayer one's Turn";
cin>>X;
switch(X){
case 1: if(board[0][0]!='O'){
board[0][0]='X';
} else {
cout<<"You can't move there\n";
continue;
}
break;
case 2: if(board[0][1]!='O'){
board[0][1]='X';
} else {
cout<<"You can't move there.\n";
continue;
}
break;
case 3:if(board[0][2]!='O'){
board[0][2]='X';
} else {
cout<<"You can't move there.\n";
continue;
}
break;
case 4: if(board[1][0]!='O'){
board[1][0]='X';
} else {
cout<<"You can't move there.\n";
continue;
}
break;
case 5:if(board[1][1]!='O'){
board[1][1]='X';
} else {
cout<<"You can't move there.\n";
continue;
}
break;
case 6:if(board[1][2]!='O'){
board[1][2]='X';
} else {
cout<<"You can't move there.\n";
continue;
}
break;
case 7: if(board[2][0]!='O'){
board[2][0]='X';
} else {
cout<<"You can't move there.\n";
continue;
}
break;
case 8:if(board[2][1]!='O'){
board[2][1]='X';
} else {
cout<<"You can't move there.\n";
continue;
}
break;
case 9:if(board[2][2]!='O'){
board[2][2]='X';
} else {
cout<<"You can't move there.\n";
continue;
}
break;
default: cout<<"Please select a value 1-9.\n";
continue;
}
for(int i=0;i<ROWS;++i){
for(int j=0;j<COLUMNS;++j){
cout<<board[i][j];
}
cout<<endl;
}
if(board[0][0]=='X'&& board[0][1]=='X'&& board[0][2]=='X' || board[1][0]=='X'&& board[1][1]=='X' && board[1][2]=='X' || board[2][0]=='X' && board[2][1]=='X' && board[2][2]=='X' || board[0][0]=='X' && board[1][0]=='X' && board[2][0]=='X' || board[0][1]=='X' && board[1][1]=='X' && board[2][1]=='X' || board[0][2]=='X' && board[1][2]=='X' && board[2][2]=='X' || board[0][0]=='X' && board[1][1]=='X' && board[2][2]=='X' || board[2][0]=='X' && board[1][1]=='X' && board[0][2]=='X' )
{
win=true;
cout<<"Player 1 wins!\n";
break;
}
if((board[0][0]=='X'||board[0][0]=='O')&&(board[0][1]=='X'||board[0][1]=='O')&&(board[0][2]=='X'|| board[0][2]=='O')&&(board[1][0]=='X'||board[1][0]=='O')&&(board[1][1]=='X'||board[1][1]=='O')&&(board[1][2]=='X'||board[1][2]=='O')&&(board[2][0]=='X'||board[2][0]=='O')&&(board[2][1]=='X'||board[2][1]=='O')&&(board[2][2]=='X'||board[2][2]=='O')){
cout<<"A draw!\n";
}
while(valid!=true){
cout<<"\nNow player 2 goes";
cin>>O;
switch(O){
case 1: if(board[0][0]!='X'){
board[0][0]='O';
valid=true;
} else {
cout<<"You can't move there\n";
continue;
}
break;
case 2: if(board[0][1]!='X'){
board[0][1]='O';
valid=true;
} else {
cout<<"You can't move there.\n";
continue;
}
break;
case 3:if(board[0][2]!='X'){
board[0][2]='O';
valid=true;
} else {
cout<<"You can't move there.\n";
continue;
}
break;
case 4: if(board[1][0]!='X'){
board[1][0]='O';
valid=true;
} else {
cout<<"You can't move there.\n";
continue;
}
break;
case 5:if(board[1][1]!='X'){
board[1][1]='O';
valid=true;
} else {
cout<<"You can't move there.\n";
continue;
}
break;
case 6:if(board[1][2]!='X'){
board[1][2]='O';
valid=true;
} else {
cout<<"You can't move there.\n";
continue;
}
break;
case 7: if(board[2][0]!='X'){
board[2][0]='O';
valid=true;
} else {
cout<<"You can't move there.\n";
continue;
}
break;
case 8:if(board[2][1]!='X'){
board[2][1]='O';
valid=true;
} else{
cout<<"You can't move there.\n";
continue;
}
break;
case 9:if(board[2][2]!='X'){
board[2][2]='O';
valid=true;
} else {
cout<<"You can't move there.\n";
continue;
}
break;
default: cout<<"Please select a value 1-9.\n";
continue;
}
}
for(int i=0;i<ROWS;++i){
for(int j=0;j<COLUMNS;++j){
cout<<board[i][j];
}
cout<<endl;
}
//Winning statements 7 ways to win a tic tac toe game
if(board[0][0]=='O' && board[0][1]=='O' && board[0][2]=='O' || board[1][0]=='O' && board[1][1]=='O' && board[1][2]=='O' || board[2][0]=='O' && board[2][1]=='O' && board[2][2]=='O' || board[0][0]=='O' && board[1][0]=='O' && board[2][0]=='O' || board[0][1]=='O' && board[1][1]=='O' && board[2][1]=='O' || board[0][2]=='O' && board[1][2]=='O' && board[2][2]=='O' || board[0][0]=='O' && board[1][1]=='O' && board[2][2]=='O' || board[2][0]=='O' && board[1][1]=='O' && board[0][2]=='O' )
{
win=true;
cout<<"Player 2 wins!\n";
break;
}
if((board[0][0]=='X'||board[0][0]=='O')&&(board[0][1]=='X'||board[0][1]=='O')&&(board[0][2]=='X'|| board[0][2]=='O')&&(board[1][0]=='X'||board[1][0]=='O')&&(board[1][1]=='X'||board[1][1]=='O')&&(board[1][2]=='X'||board[1][2]=='O')&&(board[2][0]=='X'||board[2][0]=='O')&&(board[2][1]=='X'||board[2][1]=='O')&&(board[2][2]=='X'||board[2][2]=='O')){
cout<<"A draw!\n";
}
}while(win!=true);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment