Skip to content

Instantly share code, notes, and snippets.

@jacob-herr
Last active January 7, 2024 22:35
Show Gist options
  • Save jacob-herr/c573d172bf6c68a3cd666ecbd66c6c1d to your computer and use it in GitHub Desktop.
Save jacob-herr/c573d172bf6c68a3cd666ecbd66c6c1d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
void printBoard(char board[3][3])
{
printf("\n\n\n %c | %c | %c \n", board[0][0], board[0][1], board[0][2] );
printf("---+---+---\n");
printf(" %c | %c | %c \n", board[1][0], board[1][1], board[1][2] );
printf("---+---+---\n");
printf(" %c | %c | %c \n", board[2][0], board[2][1], board[2][2] );
}
void resetBoard(char board[3][3])
{
for(int i=0; i < 3; i++)
{
for(int j=0; j < 3; j++){
board[i][j] = ' ';
}
}
}
int getnum(numchar,num)
{
while(true){
printf("what %c value? \n",numchar);
scanf("%d",&num);
if(num>3){
printf("That doesnt exist silly \n");
continue;
} else {
break;
}
}
return num;
}
void placeInput(char board[3][3],int x,int y, char player, char computer)
{
srand(time(0));
board[y][x] = player;
int computer_x,computer_y;
//the "computer" is 2 random numbers, checks that chosen space is empty
while(true){
computer_y = rand() % 3;
computer_x = rand() % 3;
if(board[computer_y][computer_x] != ' '){
continue;
} else {
board[computer_y][computer_x] = computer;
break;
}
}
}
//brute forces through the winstates
bool checkWin(char board[3][3], char player)
{
bool threeIn_a_Row = false;
for(int i = 0; i<3; i++){
if (board[i][0] == player && board[i][1] == player && board[i][2] == player){
threeIn_a_Row = true;
break;
}
else if(board[0][i] == player && board[1][i] == player && board[2][i] == player){
threeIn_a_Row = true;
break;
}
else if (board[0][0] == player && board[1][1] == player && board[2][2] == player){
threeIn_a_Row = true;
break;
}
else if (board[2][0] == player && board[1][1] == player && board[0][2] == player){
threeIn_a_Row = true;
break;
}
else{
continue;
}
}
return threeIn_a_Row;
}
int main() {
char restart;
char board[3][3];
const char player = '0';
const char computer = 'X';
char reset;
int X,Y;
bool gameover = false;
resetBoard(board);
printBoard(board);
while(true){
X = getnum('X',X)-1;
Y = getnum('Y',Y)-1;
//doesnt allow the player to play on an occupied space
while(true){
if(board[Y][X] != ' '){
printf("That spot is taken !\n");
X = getnum('X',X)-1;
Y = getnum('Y',Y)-1;
}else{
break;
}
}
placeInput(board,X,Y,player,computer);
printBoard(board);
if (checkWin(board,computer)){
printf("COMPUTER WINS!!\n");
gameover = true;
}
else if (checkWin(board,player)){
printf("PLAYER WINS!!\n");
gameover = true;
}
if(gameover){
printf("(R) to play again and anything else to quit :\n");
scanf("%s", &restart);
if(toupper(restart) == 'R'){
gameover = false;
resetBoard(board);
printBoard(board);
continue;
} else{
break;
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment