From 0976b24cad90547fec8a1252f7e962b13d711501 Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Sat, 26 Apr 2025 23:57:07 -0400 Subject: solver and prompter --- .clang-format | 3 ++ .clang-tidy | 4 +++ .gitignore | 5 +++ .vscode/launch.json | 25 +++++++++++++++ .vscode/tasks.json | 13 ++++++++ project-config.json | 13 ++++++++ src/consts.h | 4 +++ src/main.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/protos.h | 4 +++ src/types.h | 1 + 10 files changed, 164 insertions(+) create mode 100644 .clang-format create mode 100644 .clang-tidy create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 project-config.json create mode 100644 src/consts.h create mode 100644 src/main.c create mode 100644 src/protos.h create mode 100644 src/types.h diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..247acf1 --- /dev/null +++ b/.clang-format @@ -0,0 +1,3 @@ +# clang format config +BasedOnStyle: Google +IndentWidth: 4 diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..293d63b --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,4 @@ +# clang tidy config +Checks: 'clang-diagnostic-*,clang-analyzer-*,bugprone-*' +WarningsAsErrors: 'true' +HeaderFilterRegex: 'include/\*\.h' diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b881cb0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# git ignores +*~ +build +.vscode/c_cpp_properties.json +.vscode/settings.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..695296e --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,25 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "vice", + "request": "launch", + "name": "Launch Vice", + "preLaunchTask": "${defaultBuildTask}" + }, + { + "type": "vice", + "request": "attach", + "name": "Attach Vice", + "hostname": "localhost", + "port": 6502, + "preLaunchTask": "${defaultBuildTask}" + }, + { + "type": "6502", + "request": "launch", + "name": "Launch 6502", + "preLaunchTask": "${defaultBuildTask}" + } + ] +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..cc11806 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,13 @@ +{ + "tasks": [ + { + "type": "vs64", + "action": "build", + "group": { + "kind": "build", + "isDefault": true + }, + "label": "build project" + } + ] +} diff --git a/project-config.json b/project-config.json new file mode 100644 index 0000000..9bca744 --- /dev/null +++ b/project-config.json @@ -0,0 +1,13 @@ +{ + "name": "ttt-c64", + "description": "Project ttt-c64", + "toolkit": "cc65", + "sources": [ + "src/main.c" + ], + "build": "debug", + "definitions": [], + "includes": [], + "args": [], + "compiler": "" +} diff --git a/src/consts.h b/src/consts.h new file mode 100644 index 0000000..b1eaab2 --- /dev/null +++ b/src/consts.h @@ -0,0 +1,4 @@ +#define EMPTY 0 +#define X 1 +#define O 2 +#define TIE 3 \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..ebccfd4 --- /dev/null +++ b/src/main.c @@ -0,0 +1,92 @@ +#include +#include + +#include "consts.h" +#include "protos.h" + +char is_board_solved(BOARD board) { + char cases[8] = {EMPTY}; + char i; + + // rows and cols + for (i = 0; i < 3; i++) { + cases[i] = board[i][0] * board[i][1] * board[i][2]; + cases[i + 3] = board[0][i] * board[1][i] * board[2][i]; + } + + // diagonals + cases[6] = board[0][0] * board[1][1] * board[2][2]; + cases[7] = board[0][2] * board[1][1] * board[2][0]; + + // check cases for winner + for (i = 0; i < 8; i++) { + if (cases[i] == 1) { + // X won + return X; + } else if (cases[i] == 8) { + // O won + return O; + } + } + + // check cases for remaining moves + for (i = 0; i < 9; i++) { + if (cases[i] == EMPTY) { + // more spaces left; game continues + return EMPTY; + } + } + + // stalemate + return TIE; +} + +void print_board(BOARD board) { + char i, j; + + for (i = 0; i < 3; i++) { + for (j = 0; j < 3; j++) { + printf("%d ", board[i][j]); + } + printf("\n"); + } +} + +int main() { + char solution = EMPTY; + char move = EMPTY; + char move_x = EMPTY; + char move_y = EMPTY; + BOARD board = {{EMPTY}, {EMPTY}, {EMPTY}}; + + while (1) { + print_board(board); + + solution = is_board_solved(board); + switch (solution) { + case X: + printf("X won"); + break; + case O: + printf("O won"); + break; + case TIE: + printf("Stalemate"); + break; + default: + printf("Enter a move..."); + } + printf("\n"); + + cursor(1); + move = cgetc() - '0' - 1; + // if move is not a number then bail + // if (move < 1 || move > 8) { invalid! } + move_x = move % 3; + move_y = move / 3; + // if move not already taken + board[move_y][move_x] = X; + } + + return 0; +} \ No newline at end of file diff --git a/src/protos.h b/src/protos.h new file mode 100644 index 0000000..b658923 --- /dev/null +++ b/src/protos.h @@ -0,0 +1,4 @@ +#include "types.h"; + +char is_board_solved(BOARD board); +void print_board(BOARD board); \ No newline at end of file diff --git a/src/types.h b/src/types.h new file mode 100644 index 0000000..0e441ed --- /dev/null +++ b/src/types.h @@ -0,0 +1 @@ +typedef char BOARD[3][3]; -- cgit v1.2.3