Skip to content

Instantly share code, notes, and snippets.

@draganczukp
Created February 27, 2018 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save draganczukp/519c77dd71903a5a03193001e21a8a65 to your computer and use it in GitHub Desktop.
Save draganczukp/519c77dd71903a5a03193001e21a8a65 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
void processInput(char in);
void newGame();
void play();
using namespace std;
bool gameover = false;
char player = 'x';
int turns = 0;
char grid[3][3] = {
{' ',' ',' '},
{' ',' ',' '},
{' ',' ',' '}
};
int main(){
char ans='y';
while(ans!='N'&&ans!='n'){
play();
cout << "Nowa Gra? (Y/n): ";
/* ans = getchar(); */
cin >> ans;
}
return 0;
}
void play(){
while(!gameover){
system("clear");
printf("%c|%c|%c\n", grid[0][0],grid[1][0],grid[2][0]);
cout <<"-----" << endl;
printf("%c|%c|%c\n", grid[0][1],grid[1][1],grid[2][1]);
cout <<"-----" << endl;
printf("%c|%c|%c\n", grid[0][2],grid[1][2],grid[2][2]);
processInput(getchar());
if(turns>=9)
gameover=false;
}
}
void processInput(char in){
int x,y;
switch(in){
case '7':
x=0;
y=0;
break;
case '8':
x=1;
y=0;
break;
case '9':
x=2;
y=0;
break;
case '4':
x=0;
y=1;
break;
case '5':
x=1;
y=1;
break;
case '6':
x=2;
y=1;
break;
case '1':
x=0;
y=2;
break;
case '2':
x=1;
y=2;
break;
case '3':
x=2;
y=2;
break;
default:
cout << "Błędne wejście" << endl;
return;
break;
}
grid[x][y] = player;
player = player=='x' ? 'o' : 'x';
turns++;
}
void newGame(){
for(int x=0;x<2;x++){
for(int y=0;y<2;y++){
grid[x][y] = ' ';
}
}
gameover = false;
turns=0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment