Finished Game. Wait for feedback
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
output
|
||||||
76
TicTacToe.c
76
TicTacToe.c
@@ -1,26 +1,47 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
char cPlayfield[3][3] = {
|
char cPlayfield[3][3] = {
|
||||||
{' ', ' ', ' '},
|
{' ', ' ', ' '},
|
||||||
{' ', ' ', ' '},
|
{' ', ' ', ' '},
|
||||||
{' ', ' ', ' '}};
|
{' ', ' ', ' '}};
|
||||||
|
|
||||||
char *ptrPlayfield;
|
char *ptrPlayfield;
|
||||||
|
char currentUser;
|
||||||
|
|
||||||
|
int running = 1; // TODO: find bool and convert to it
|
||||||
|
int counter = 1;
|
||||||
|
|
||||||
void renderPlayfield();
|
void renderPlayfield();
|
||||||
|
void getUserInput();
|
||||||
|
void checkForWin();
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
while (running == 1)
|
||||||
|
{
|
||||||
renderPlayfield();
|
renderPlayfield();
|
||||||
|
currentUser = counter++ % 2 == 1 ? 'X' : 'O';
|
||||||
|
getUserInput();
|
||||||
|
checkForWin();
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void renderPlayfield() {
|
void renderPlayfield()
|
||||||
|
{
|
||||||
|
#ifdef WIN32
|
||||||
|
system("cls");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef linux
|
||||||
|
system("clear");
|
||||||
|
#endif
|
||||||
|
|
||||||
printf(" | 1 | 2 | 3 |\n");
|
printf(" | 1 | 2 | 3 |\n");
|
||||||
printf("---------------\n");
|
printf("---------------\n");
|
||||||
for (int i = 0; i < 3; i++)
|
for (int i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
printf("%d | ", i+1);
|
printf("%d | ", i + 1);
|
||||||
for (int j = 0; j < 3; j++)
|
for (int j = 0; j < 3; j++)
|
||||||
{
|
{
|
||||||
ptrPlayfield = &cPlayfield[i][j];
|
ptrPlayfield = &cPlayfield[i][j];
|
||||||
@@ -28,5 +49,52 @@ void renderPlayfield() {
|
|||||||
}
|
}
|
||||||
printf("\n---------------\n");
|
printf("\n---------------\n");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void getUserInput()
|
||||||
|
{
|
||||||
|
int currentCol = 0;
|
||||||
|
int currentRow = 0;
|
||||||
|
printf("\nCurrent Player: %c\n", currentUser);
|
||||||
|
printf("Row and Colum: ");
|
||||||
|
scanf("%d %d", ¤tRow, ¤tCol);
|
||||||
|
|
||||||
|
if (currentRow < 1 || currentRow > 3 || currentCol < 1 || currentCol > 3)
|
||||||
|
{
|
||||||
|
printf("Your Input is out of Range. Use only numbers 1, 2 or 3. \n");
|
||||||
|
return getUserInput();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cPlayfield[currentRow - 1][currentCol - 1] = currentUser;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void checkForWin() {
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
if (cPlayfield[i][0] == cPlayfield[i][1] && cPlayfield[i][1] == cPlayfield[i][2] && cPlayfield[i][2] != ' ') {
|
||||||
|
running = 0;
|
||||||
|
printf("The winner is: %c\n", cPlayfield[i][0]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cPlayfield[0][i] == cPlayfield[1][i] && cPlayfield[1][i] == cPlayfield[2][i] && cPlayfield[2][i] != ' ') {
|
||||||
|
running = 0;
|
||||||
|
printf("The winner is: %c\n", cPlayfield[0][i]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if (cPlayfield[0][0] == cPlayfield[1][1] && cPlayfield[1][1] == cPlayfield[2][2] && cPlayfield[2][2] != ' ') {
|
||||||
|
running = 0;
|
||||||
|
printf("The winner is %c\n", cPlayfield[0][0]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cPlayfield[0][2] == cPlayfield[1][1] && cPlayfield[1][1] == cPlayfield[2][0] && cPlayfield[2][0] != ' ') {
|
||||||
|
running = 0;
|
||||||
|
printf("The winner is %d\n", cPlayfield[0][2]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user