draws and ncurses WIP - same code lol

このコミットが含まれているのは:
woosh 2023-11-12 11:43:33 +00:00
コミット e35d4165d0
1個のファイルの変更39行の追加21行の削除

60
uttt.c
ファイルの表示

@ -4,6 +4,7 @@
#include <stdlib.h>
#include <string.h>
#include <curses.h>
#include <signal.h>
struct ut_state
{
@ -12,6 +13,12 @@ struct ut_state
int playBoard;
char player;
};
const struct ut_state ut_initial = {
.boards = {0},
.tiles = {0},
.playBoard = -1,
.player = 'X'
};
char ut_turn(char player)
@ -45,22 +52,20 @@ char ut_winner(char *tiles, int offset, int stride) {
// diagonalz
char tile = T(1, 1);
if (tile == '\0') {return '\0';}
if (tile == T(0, 0) && tile == T(2, 2)) {return tile;}
if (tile == T(2, 0) && tile == T(0, 2)) {return tile;}
// check for draw
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
if (T(x, y) == '\0') {
// tile is empty - not a draw
return '\0';
}
}
if (tile != '\0')
{
if (tile == T(0, 0) && tile == T(2, 2)) {return tile;}
if (tile == T(2, 0) && tile == T(0, 2)) {return tile;}
}
// draw
return -1;
for(int y = 0; y < 3; y++)
{
for(int x = 0; x < 3; x++)
{
if(T(y, x) == '\0') {return '\0';} // in progress
}
}
return ' '; // draw
}
int ut_move(struct ut_state *new_state, const struct ut_state *old_state, int row, int col)
@ -160,13 +165,19 @@ bool getpos(const struct ut_state *state, int *x, int *y) {
return true;
}
void finish(int sig)
{
endwin();
// other cleanup
exit(0);
}
int main(int *argc, char **argv) {
struct ut_state state = {
.boards = {0},
.tiles = {0},
.playBoard = -1,
.player = 'X'
};
signal(SIGINT, finish);
/*initscr();
keypad(stdscr, TRUE);*/
struct ut_state state = ut_initial;
while (true) {
int x, y;
ut_show(&state);
@ -177,7 +188,14 @@ int main(int *argc, char **argv) {
char w = ut_winner((char *)state.boards, 0, 3);
if(w)
{
printf("\n%c Wins!\n", (int)w);
if(w == ' ')
{
printf("\nDraw!\n");
}
else
{
printf("\n%c wins!\n", (int)w);
}
return 0;
}
}