mirror of
https://github.com/JannisHeydemann/BoredOS.git
synced 2026-05-30 02:16:58 +00:00
started dev on TicTacToe
This commit is contained in:
117
src/userland/games/tictactoe.c
Normal file
117
src/userland/games/tictactoe.c
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
// Copyright (C) 2026 Jannis Heydemann
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <libc/libui.h>
|
||||||
|
|
||||||
|
#define WINDOW_W 400
|
||||||
|
#define WINDOW_H 400
|
||||||
|
|
||||||
|
char cPlayfield[3][3] = {
|
||||||
|
{' ', ' ', ' '},
|
||||||
|
{' ', ' ', ' '},
|
||||||
|
{' ', ' ', ' '}};
|
||||||
|
|
||||||
|
char *ptrPlayfield;
|
||||||
|
char currentUser;
|
||||||
|
|
||||||
|
int running = 1;
|
||||||
|
int counter = 1;
|
||||||
|
|
||||||
|
void renderPlayfield();
|
||||||
|
void getUserInput();
|
||||||
|
void checkForWin();
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
while (running == 1)
|
||||||
|
{
|
||||||
|
renderPlayfield();
|
||||||
|
currentUser = counter++ % 2 == 1 ? 'X' : 'O';
|
||||||
|
getUserInput();
|
||||||
|
checkForWin();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
for (int j = 0; j < 3; j++)
|
||||||
|
{
|
||||||
|
ptrPlayfield = &cPlayfield[i][j];
|
||||||
|
printf("%c | ", *ptrPlayfield);
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
renderPlayfield();
|
||||||
|
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;
|
||||||
|
renderPlayfield();
|
||||||
|
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;
|
||||||
|
renderPlayfield();
|
||||||
|
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;
|
||||||
|
renderPlayfield();
|
||||||
|
printf("The winner is %d\n", cPlayfield[0][2]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user