Difference between revisions of "Tic Tac Toe"

From GbdevWiki
Jump to: navigation, search
(Created page with 'Tic Tac Toe know in the uk as noughts and crosses please feel free to fix any bugs you find or add gamemodes etc: #include <stdio.h> #include <gb/gb.h> #define MAXX 3 #defi…')
 
(Blanked the page)
Line 1: Line 1:
/*
 
Tic Tac Toe
 
know in the uk as
 
noughts and crosses
 
please feel free to fix
 
any bugs you find or add gamemodes etc
 
*/
 
#include <stdio.h>
 
#include <gb/gb.h>
 
  
#define MAXX 3
 
#define MAXY 4
 
 
unsigned char player;
 
unsigned char running = 1;
 
unsigned char name1[10];//player 1's name
 
unsigned char name2[10]; //player 2's name
 
unsigned char Map[MAXX][MAXY] = {{46, 46, 46, 10},
 
{46, 46, 46, 10},
 
{46, 46, 46, 10}};// 46 = "." 10 = '\n
 
void title();
 
void You_Won();
 
void clear_screen();
 
void check_win();//check if a player has won
 
void print_Map();
 
void Enter_Name();
 
void Set_Map(unsigned char x, unsigned char y, unsigned char k);
 
 
void title(){
 
printf("Noughts and crosses!\n");
 
printf("        xoo          \n");
 
printf("        oxx          \n");
 
printf("        ..x          \n");
 
printf("    Press Start      \n");
 
waitpad(J_START);
 
clear_screen();
 
return;
 
}
 
 
void You_Won(){
 
clear_screen();
 
if (player == 1){
 
printf("==================\n");
 
printf("=                =\n");
 
printf("=                =\n");
 
printf("=    %s WINS!!!  \n", name1);
 
printf("=    %s LOSES :(  \n", name2);
 
printf("=                =\n");
 
printf("=                =\n");
 
printf("==================\n");
 
printf("Press Start to reset!\n");
 
waitpad(J_START);
 
reset();
 
}
 
else{
 
printf("==================\n");
 
printf("=                =\n");
 
printf("=                =\n");
 
printf("=    %s WINS!!!  \n", name2);
 
printf("=    %s LOSES :(  \n", name1);
 
printf("=                =\n");
 
printf("=                =\n");
 
printf("==================\n");
 
printf("Press Start to reset\n");
 
waitpad(J_START);
 
reset();
 
}
 
return;
 
}
 
 
void check_win(){
 
/*
 
x..
 
x..
 
x..
 
*/
 
if (Map[0][0] == 88 && Map[1][0] == 88 && Map[2][0] == 88){player = 1;You_Won();}
 
else if (Map[0][0] == 48 && Map[1][0] == 48 && Map[2][0] == 48){player = 2;You_Won();}
 
/*
 
.x.
 
.x.
 
.x.
 
*/
 
else if (Map[0][1] == 88 && Map[1][1] == 88 && Map[2][1] == 88){player = 1;You_Won();}
 
else if (Map[0][0] == 48 && Map[1][0] == 48 && Map[2][0] == 48){player = 2;You_Won();}
 
/*
 
..x
 
..x
 
..x
 
*/
 
else if (Map[0][2] == 88 && Map[1][2] == 88 && Map[2][2] == 88){player = 1;You_Won();}
 
else if (Map[0][2] == 48 && Map[1][2] == 48 && Map[2][2] == 48){player = 2;You_Won();}
 
/*
 
xxx
 
...
 
...
 
*/
 
else if (Map[0][0] == 88 && Map[0][1] == 88 && Map[0][2] == 88){player = 1;You_Won();}
 
else if (Map[0][0] == 48 && Map[0][1] == 48 && Map[0][2] == 48){player = 2;You_Won();}
 
/*
 
...
 
xxx
 
...
 
*/
 
else if (Map[1][0] == 88 && Map[1][1] == 88 && Map[1][2] == 88){player = 1;You_Won();}
 
else if (Map[1][0] == 48 && Map[1][1] == 48 && Map[1][2] == 48){player = 2;You_Won();}
 
/*
 
...
 
...
 
xxx
 
*/
 
else if (Map[2][0] == 88 && Map[2][1] == 88 && Map[2][2] == 88){player = 1;You_Won();}
 
else if (Map[2][0] == 48 && Map[2][1] == 48 && Map[2][2] == 48){player = 2;You_Won();}
 
/*
 
x..
 
.x.
 
..x
 
*/
 
else if (Map[0][0] == 88 && Map[1][1] == 88 && Map[2][2] == 88){player = 1;You_Won();}
 
else if (Map[0][0] == 48 && Map[1][1] == 48 && Map[2][2] == 48){player = 2;You_Won();}
 
/*
 
..x
 
.x.
 
x..
 
*/
 
else if (Map[0][2] == 88 && Map[1][1] == 88 && Map[2][0] == 88){player = 1;You_Won();}
 
else if (Map[0][2] == 48 && Map[1][2] == 48 && Map[3][2] == 48){player = 2;You_Won();}
 
return;
 
}
 
 
void Set_Map(unsigned char x, unsigned char y, unsigned char k){Map[x][y] = k;return;}
 
 
void Enter_Name(){
 
unsigned char i = 0;
 
unsigned char k = 0;
 
    clear_screen();
 
printf("Player 1 Enter Your Name\n\"*\" To end\nPress A To Select\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
 
while(i <= 10){
 
i--;
 
    if(name1[i] == 42){name1[i] = 0;break;}
 
i++;
 
name1[i] = getchar();
 
printf("Last Character: %c\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", name1[i]);
 
i++;
 
}
 
    printf("\nNow Player 2\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
 
i = 0;
 
while(i <= 10){
 
    i--;
 
    if(name2[i] == 42){name2[i] = 0;break;}
 
i++;
 
name2[i] = getchar();
 
printf("Last Character: %c\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", name2[i]);
 
i++;
 
}
 
clear_screen();
 
return;
 
}
 
 
void print_Map(){
 
unsigned char i;
 
unsigned char k;
 
unsigned char c;
 
for (i = 0;i<MAXX;i++){
 
for (k = 0;k<MAXY;k++){
 
    c = Map[i][k];
 
printf("%c", c);
 
}
 
}
 
return;
 
}
 
void clear_screen(){unsigned char i = 0;while (i < 18){printf("\n");i++;}return;}
 
 
int main(){
 
unsigned char tile_x;
 
unsigned char tile_y;
 
player = 1;
 
title();
 
Enter_Name();
 
while(running == 1){
 
if (player == 1){
 
start1:
 
printf("\n%s\nPlease Enter The\nX Cord\n\n\n\n\n\n\n\n\n\n", name1);
 
while(1){tile_x = getchar();if (tile_x <= 48 || tile_x > 51){printf("\nEnter a number\nbetween 1 and 9\n\n\n\n\n\n\n\n\n\n\n\n");}else{break;}}
 
tile_x = tile_x - 48;
 
printf("Please Enter The\nY Cord\n\n\n\n\n\n\n\n\n\n\n");
 
while(1){tile_y = getchar();if (tile_y <= 48 || tile_y > 51){printf("\nEnter a number\nbetween 1 and 9\n\n\n\n\n\n\n\n\n\n\n\n");}else{break;}}
 
tile_y = tile_y - 48;
 
tile_x--;tile_y--;
 
if (Map[tile_x][tile_y] == 48 || Map[tile_x][tile_y] == 88){goto start1;}
 
Map[tile_x][tile_y] = 88;//X in ASCII
 
player = 2;
 
}
 
else if (player == 2){
 
start2:
 
printf("\n%s\nPlease Enter The\nX Cord\n\n\n\n\n\n\n\n\n\n", name2);
 
while(1){tile_x = getchar();if (tile_x < 48 || tile_x > 51){printf("\nEnter a number\nbetween 1 and 9\n\n\n\n\n\n\n\n\n\n\n\n");}else{break;}}
 
tile_x = tile_x - 48;
 
printf("Please Enter The\nY Cord\n\n\n\n\n\n\n\n\n\n\n");
 
while(1){tile_y = getchar();if (tile_y < 48 || tile_y > 51){printf("\nEnter a number\nbetween 1 and 9\n\n\n\n\n\n\n\n\n\n\n\n");}else{break;}}
 
tile_y = tile_y - 48;
 
tile_x--;tile_y--;
 
if (Map[tile_x][tile_y] == 48 || Map[tile_x][tile_y] == 88){printf("\nTile Already Taken!\n\n\n\n\n\n\n\n\n\n\n\n");delay(1300);goto start2;}
 
Map[tile_x][tile_y] = 48;//0 in ASCII
 
player = 1;
 
}
 
    clear_screen();
 
print_Map();
 
check_win();//check for 3 in a row
 
}
 
 
return 0;
 
}
 

Revision as of 13:36, 1 August 2014