Finished Game. Wait for feedback

This commit is contained in:
Jannis Heydemann
2026-05-13 09:18:35 +02:00
parent 06f1cc941f
commit e97df2712e
3 changed files with 74 additions and 5 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
output

View File

@@ -1,26 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
char cPlayfield[3][3] = {
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '}};
char *ptrPlayfield;
char currentUser;
int running = 1; // TODO: find bool and convert to it
int counter = 1;
void renderPlayfield();
void getUserInput();
void checkForWin();
int main()
{
renderPlayfield();
while (running == 1)
{
renderPlayfield();
currentUser = counter++ % 2 == 1 ? 'X' : 'O';
getUserInput();
checkForWin();
}
return 0;
}
void renderPlayfield() {
void renderPlayfield()
{
#ifdef WIN32
system("cls");
#endif
#ifdef linux
system("clear");
#endif
printf(" | 1 | 2 | 3 |\n");
printf("---------------\n");
for (int i = 0; i < 3; i++)
{
printf("%d | ", i+1);
printf("%d | ", i + 1);
for (int j = 0; j < 3; j++)
{
ptrPlayfield = &cPlayfield[i][j];
@@ -28,5 +49,52 @@ void renderPlayfield() {
}
printf("\n---------------\n");
}
}
void getUserInput()
{
int currentCol = 0;
int currentRow = 0;
printf("\nCurrent Player: %c\n", currentUser);
printf("Row and Colum: ");
scanf("%d %d", &currentRow, &currentCol);
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;
}
}

BIN
main

Binary file not shown.