Last active
August 9, 2023 17:50
-
-
Save ghulamMustafaRaza/94fdb448c7aeb6aa2fa6e7b460a762f1 to your computer and use it in GitHub Desktop.
c practice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cs50.h> | |
#include <stdio.h> | |
#include <string.h> | |
char selectWinner(char board[3][3], char players[2]); | |
void printBoard(char board[3][3]); | |
int main(void) | |
{ | |
printf("Hey, welcome to tic toc toe, rules are simple, there are three rows (1,2,3), and three cols (1,2,3) everytime we ask you for the input do select one of em between, a1-c3. \n"); | |
// row a | |
// array | |
char row1[3] = {'a', 'b', 'c'} ; | |
char board[3][3] = { | |
{' ', ' ', ' '}, // row1 index = 0 | |
{' ', ' ', ' '}, | |
{' ', ' ', ' '} | |
}; | |
// Possible Players | |
char players[2] = {'x', 'o'}; | |
char winner = ' '; | |
int currentTurn = 0; // it is meant be between 0 to 8 | |
while (winner == ' ' && currentTurn < 9) | |
{ | |
printf("Currently it's %c's turn, select your position. \n", players[currentTurn % 2]); // reminder or modulus | |
printBoard(board); | |
int row = get_char("Select row (a,b or c): ") - 97; | |
int col = get_int("Select col (1,2 or 3): ") - 1; | |
printf("You selected row: %i, col: %i with current value %c spot.\n", row, col, board[row][col]); | |
if (board[row][col] == ' ') | |
{ | |
board[row][col] = players[currentTurn % 2]; | |
currentTurn++; | |
winner = selectWinner(board, players); // ' ' or 'x' or 'o' | |
} | |
else | |
{ | |
printf("You selected wrong spot.\n"); | |
} | |
}; | |
printBoard(board); | |
if (winner == ' ') | |
{ | |
printf("it's a draw.\n"); | |
} | |
else | |
{ | |
printf("%c wins.\n", winner); | |
} | |
} | |
void printBoard(char board[3][3]) | |
{ | |
printf(" 1 2 3 \n"); | |
printf("a. %c | %c | %c \n", board[0][0], board[0][1], board[0][2]); | |
printf(" ---------\n"); | |
printf("b. %c | %c | %c \n", board[1][0], board[1][1], board[1][2]); | |
printf(" ---------\n"); | |
printf("c. %c | %c | %c \n", board[2][0], board[2][1], board[2][2]); | |
} | |
char selectWinner(char board[3][3], char players[2]) | |
{ | |
char result = ' '; | |
for (int i = 0; i < 2; i++) | |
{ | |
char p = players[i]; | |
if (board[0][0] == p && board[0][1] == p && board[0][2] == p) | |
{ | |
return p; | |
} | |
else if (board[1][0] == p && board[1][1] == p && board[1][2] == p) | |
{ | |
return p; | |
} | |
else if (board[2][0] == p && board[2][1] == p && board[2][2] == p) | |
{ | |
return p; | |
} | |
else if (board[0][0] == p && board[1][0] == p && board[2][0] == p) | |
{ | |
return p; | |
} | |
else if (board[0][1] == p && board[1][1] == p && board[2][1] == p) | |
{ | |
return p; | |
} | |
else if (board[0][2] == p && board[1][2] == p && board[2][2] == p) | |
{ | |
return p; | |
} | |
else if (board[0][0] == p && board[1][1] == p && board[2][2] == p) | |
{ | |
return p; | |
} | |
else if (board[0][2] == p && board[1][1] == p && board[2][0] == p) | |
{ | |
return p; | |
}; | |
}; | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cs50.h> | |
#include <stdio.h> | |
#include <string.h> | |
void game(void); | |
char selectWinner(char board[3][3], char players[2]); | |
void printBoard(char board[3][3]); | |
int main(void) | |
{ | |
bool replay = true; | |
printf("Hey, welcome to tic toc toe, rules are simple, there are three rows (1,2,3), and three cols (1,2,3) everytime we ask you for the input do select one of em between, a1-c3. \n"); | |
while (replay) | |
{ | |
game(); | |
replay = get_char("Wanna replay? type (y) ") == 'y'; | |
} | |
printf("Good Game.Thanks for playing \n"); | |
} | |
void game(void) | |
{ | |
// row a | |
// array | |
char row1[3] = {'a', 'b', 'c'}; | |
char board[3][3] = {{' ', ' ', ' '}, // row1 index = 0 | |
{' ', ' ', ' '}, | |
{' ', ' ', ' '}}; | |
// Possible Players | |
char players[2] = {'x', 'o'}; | |
char winner = ' '; | |
int currentTurn = 0; // it is meant be between 0 to 8 | |
while (winner == ' ' && currentTurn < 9) | |
{ | |
printf("Currently it's %c's turn, select your position. \n", players[currentTurn % 2]); // reminder or modulus | |
printBoard(board); | |
int row = get_char("Select row (a,b or c): ") - 97; | |
int col = get_int("Select col (1,2 or 3): ") - 1; | |
printf("You selected row: %i, col: %i with current value %c spot.\n", row, col, board[row][col]); | |
if (board[row][col] == ' ') | |
{ | |
board[row][col] = players[currentTurn % 2]; | |
currentTurn++; | |
winner = selectWinner(board, players); // ' ' or 'x' or 'o' | |
} | |
else | |
{ | |
printf("You selected wrong spot.\n"); | |
} | |
}; | |
printBoard(board); | |
if (winner == ' ') | |
{ | |
printf("it's a draw.\n"); | |
} | |
else | |
{ | |
printf("%c wins.\n", winner); | |
} | |
} | |
void printBoard(char board[3][3]) | |
{ | |
printf(" 1 2 3 \n"); | |
printf("a. %c | %c | %c \n", board[0][0], board[0][1], board[0][2]); | |
printf(" ---------\n"); | |
printf("b. %c | %c | %c \n", board[1][0], board[1][1], board[1][2]); | |
printf(" ---------\n"); | |
printf("c. %c | %c | %c \n", board[2][0], board[2][1], board[2][2]); | |
} | |
char selectWinner(char board[3][3], char players[2]) | |
{ | |
char result = ' '; | |
for (int i = 0; i < 2; i++) | |
{ | |
char p = players[i]; | |
if (board[0][0] == p && board[0][1] == p && board[0][2] == p) | |
{ | |
return p; | |
} | |
else if (board[1][0] == p && board[1][1] == p && board[1][2] == p) | |
{ | |
return p; | |
} | |
else if (board[2][0] == p && board[2][1] == p && board[2][2] == p) | |
{ | |
return p; | |
} | |
else if (board[0][0] == p && board[1][0] == p && board[2][0] == p) | |
{ | |
return p; | |
} | |
else if (board[0][1] == p && board[1][1] == p && board[2][1] == p) | |
{ | |
return p; | |
} | |
else if (board[0][2] == p && board[1][2] == p && board[2][2] == p) | |
{ | |
return p; | |
} | |
else if (board[0][0] == p && board[1][1] == p && board[2][2] == p) | |
{ | |
return p; | |
} | |
else if (board[0][2] == p && board[1][1] == p && board[2][0] == p) | |
{ | |
return p; | |
}; | |
}; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment