standards ish.
This commit is contained in:
parent
d42990d57b
commit
1db219f5f1
38
src/main.cpp
38
src/main.cpp
@ -20,9 +20,9 @@ struct game_state {
|
||||
char board[TILE_COUNT];
|
||||
};
|
||||
|
||||
int check_win_condition(struct game_state * gs, char player) {
|
||||
int check_win_condition(game_state * gs, char player) {
|
||||
int i;
|
||||
for(i=0;i<3;i++){
|
||||
for( i = 0 ; i < 3 ; i++){
|
||||
//rows
|
||||
if(gs->board[(i*3)]==player && gs->board[(i*3)+1]==player && gs->board[(i*3)+2]==player)
|
||||
return 1;
|
||||
@ -38,12 +38,12 @@ int check_win_condition(struct game_state * gs, char player) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void display_game_state(struct game_state * gs){
|
||||
void display_game_state(game_state * gs){
|
||||
printf("turn %i:\n",gs->turn);
|
||||
for(int i=0;i<3;i++){
|
||||
for(int j=0;j<3;j++){
|
||||
for(int i = 0; i < 3; i++){
|
||||
for(int j = 0; j < 3 ; j++){
|
||||
if(gs->board[(i*3)+j])
|
||||
printf("%c",gs->board[(i*3)+j]);
|
||||
printf("%c", gs->board[(i*3)+j]);
|
||||
else
|
||||
printf("_");
|
||||
}
|
||||
@ -51,31 +51,31 @@ void display_game_state(struct game_state * gs){
|
||||
}
|
||||
}
|
||||
|
||||
void player_turn(struct game_state * gs, char player, int input)
|
||||
void player_turn(game_state * gs, char player, int input)
|
||||
{
|
||||
if(input > TILE_COUNT && input < 0)
|
||||
fprintf(stderr,"ERR: player input not in range.");
|
||||
if(!gs->board[input])
|
||||
gs->board[input]=player;
|
||||
gs->board[input] = player;
|
||||
if(check_win_condition(gs,player)){
|
||||
printf("player %c wins!\n",player);
|
||||
gs->turn=-1;
|
||||
printf("player %c wins!\n", player);
|
||||
gs->turn = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void game_turn(struct game_state * gs,int player_input){
|
||||
void game_turn(game_state * gs,int player_input){
|
||||
gs->turn++;
|
||||
player_turn(gs,'O',player_input);
|
||||
player_turn(gs, 'O', player_input);
|
||||
display_game_state(gs);
|
||||
if(gs->turn==-1)
|
||||
if(gs->turn == -1)
|
||||
return;
|
||||
player_turn(gs,'X',rand()%TILE_COUNT);
|
||||
player_turn(gs, 'X', rand()%TILE_COUNT);
|
||||
display_game_state(gs);
|
||||
}
|
||||
|
||||
int exists_empty_tile(struct game_state * gs){
|
||||
int exists_empty_tile(game_state * gs){
|
||||
int i;
|
||||
for(i=0;i<TILE_COUNT;i++)
|
||||
for( i = 0; i < TILE_COUNT ; i++)
|
||||
if(!gs->board[i])
|
||||
return 1;
|
||||
return 0;
|
||||
@ -83,8 +83,8 @@ int exists_empty_tile(struct game_state * gs){
|
||||
|
||||
int main(){
|
||||
srand(time(NULL));
|
||||
struct game_state gs = {0,0};
|
||||
while(exists_empty_tile(&gs) && gs.turn!=-1){
|
||||
game_turn(&gs,get_player_input());
|
||||
game_state gs = {0,0};
|
||||
while(exists_empty_tile(&gs) && gs.turn != -1){
|
||||
game_turn(&gs, get_player_input());
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user