mirror of
https://github.com/nelsbrock/dev-tictactoe.git
synced 2026-08-14 20:56:49 +02:00
Compare commits
11
Commits
791e1a9e97
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
610bc1c07c
|
||
|
|
12eba69eb1
|
||
|
|
2b55717595
|
||
|
|
504c950c15
|
||
|
|
ebaf64982d
|
||
|
|
ac4d4eb4fa
|
||
|
|
4520ba5b61
|
||
|
|
1da2264296
|
||
|
|
34f309404c
|
||
|
|
229f57a888
|
||
|
|
ae2b0c6232
|
@@ -0,0 +1,10 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
end_of_line = lf
|
||||
charset = utf-8
|
||||
insert_final_newline = true
|
||||
|
||||
[{*.{c,h},Kbuild,Makefile}]
|
||||
indent_style = tab
|
||||
indent_size = 8
|
||||
@@ -22,13 +22,13 @@ After loading the module, player X can write grid coordinates
|
||||
(ranging from 1 to 3, format `XY`) to `/dev/tictactoe` to make their first move:
|
||||
|
||||
```console
|
||||
# echo 12 > /dev/tictactoe
|
||||
# echo 22 > /dev/tictactoe
|
||||
```
|
||||
|
||||
After player X has made his move, player O can do the same:
|
||||
After player X has made their move, player O can do the same:
|
||||
|
||||
```console
|
||||
# echo 22 > /dev/tictactoe
|
||||
# echo 13 > /dev/tictactoe
|
||||
```
|
||||
|
||||
To inspect the current state of the game, simply read from `/dev/tictactoe`:
|
||||
@@ -39,9 +39,9 @@ To inspect the current state of the game, simply read from `/dev/tictactoe`:
|
||||
|
||||
```
|
||||
#####
|
||||
# X #
|
||||
# O #
|
||||
# #
|
||||
# X #
|
||||
#O #
|
||||
#####
|
||||
|
||||
It's X's turn!
|
||||
|
||||
+81
-61
@@ -4,84 +4,91 @@
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/printk.h>
|
||||
|
||||
int tictactoe_game_init(ttt_game_t *game)
|
||||
/*
|
||||
* Initializes a new game instance.
|
||||
*/
|
||||
void tictactoe_game_init(ttt_game_t *game)
|
||||
{
|
||||
game->next_turn = 'X';
|
||||
game->winner = 0;
|
||||
for (int x = 0; x < 3; x++) {
|
||||
for (int y = 0; y < 3; y++) {
|
||||
for (size_t x = 0; x < 3; x++) {
|
||||
for (size_t y = 0; y < 3; y++) {
|
||||
game->board[x][y] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// returns 0 if there is no winner yet, -1 if it's a tie, and the respective player if they win
|
||||
char tictactoe_game_check_winner(ttt_game_t *game)
|
||||
/*
|
||||
* Steps over the board, starting at position (start_x, start_y) and ending
|
||||
* at the board's boundaries, checking if all cells are occupied by the same
|
||||
* player.
|
||||
*
|
||||
* start_x and start_y must be within the board's boundaries.
|
||||
*
|
||||
* Returns the respective player, or '\x00' if no single player occupies all
|
||||
* cells within the specified streak.
|
||||
*/
|
||||
static char tictactoe_game_check_streak(ttt_game_t *game, size_t start_x,
|
||||
size_t start_y, ssize_t step_x,
|
||||
ssize_t step_y)
|
||||
{
|
||||
char winner;
|
||||
size_t x;
|
||||
size_t y;
|
||||
|
||||
winner = game->board[start_x][start_y];
|
||||
x = start_x + step_x;
|
||||
y = start_y + step_y;
|
||||
|
||||
if (winner == ' ')
|
||||
return 0;
|
||||
|
||||
while (x >= 0 && x < 3 && y >= 0 && y < 3) {
|
||||
if (game->board[x][y] != winner)
|
||||
return 0;
|
||||
x += step_x;
|
||||
y += step_y;
|
||||
}
|
||||
|
||||
return winner;
|
||||
}
|
||||
|
||||
/*
|
||||
* Checks for a winner with the current board configuration.
|
||||
*
|
||||
* Returns
|
||||
* '\x00' if the game has not ended,
|
||||
* '\xFF' if the game is tied, or
|
||||
* the winner of the game.
|
||||
*/
|
||||
static char tictactoe_game_check_winner(ttt_game_t *game)
|
||||
{
|
||||
char winner;
|
||||
|
||||
// check columns
|
||||
for (int x = 0; x < 3; x++) {
|
||||
winner = game->board[x][0];
|
||||
if (winner != ' ') {
|
||||
for (int y = 1; y < 3; y++) {
|
||||
if (game->board[x][y] != winner) {
|
||||
winner = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (winner != 0)
|
||||
return winner;
|
||||
}
|
||||
for (size_t x = 0; x < 3; x++) {
|
||||
if ((winner = tictactoe_game_check_streak(game, x, 0, 0, 1)))
|
||||
return winner;
|
||||
}
|
||||
|
||||
// check rows
|
||||
for (int y = 0; y < 3; y++) {
|
||||
winner = game->board[0][y];
|
||||
if (winner != ' ') {
|
||||
for (int x = 1; x < 3; x++) {
|
||||
if (game->board[x][y] != winner) {
|
||||
winner = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (winner != 0)
|
||||
return winner;
|
||||
}
|
||||
for (size_t y = 0; y < 3; y++) {
|
||||
if ((winner = tictactoe_game_check_streak(game, 0, y, 1, 0)))
|
||||
return winner;
|
||||
}
|
||||
|
||||
// check diagonal top-left <-> bottom-right
|
||||
winner = game->board[0][0];
|
||||
if (winner != ' ') {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (game->board[i][i] != winner) {
|
||||
winner = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (winner != 0)
|
||||
return winner;
|
||||
}
|
||||
if ((winner = tictactoe_game_check_streak(game, 0, 0, 1, 1)))
|
||||
return winner;
|
||||
|
||||
// check diagonal bottom-left <-> top-right
|
||||
winner = game->board[2][0];
|
||||
if (winner != ' ') {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (game->board[2 - i][i] != winner) {
|
||||
winner = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (winner != 0)
|
||||
return winner;
|
||||
}
|
||||
if ((winner = tictactoe_game_check_streak(game, 0, 2, 1, -1)))
|
||||
return winner;
|
||||
|
||||
// check for tie
|
||||
winner = -1;
|
||||
for (int x = 0; x < 3; x++) {
|
||||
for (int y = 0; y < 3; y++) {
|
||||
winner = '\xFF';
|
||||
for (size_t x = 0; x < 3; x++) {
|
||||
for (size_t y = 0; y < 3; y++) {
|
||||
if (game->board[x][y] == ' ') {
|
||||
winner = 0;
|
||||
break;
|
||||
@@ -92,14 +99,24 @@ char tictactoe_game_check_winner(ttt_game_t *game)
|
||||
return winner;
|
||||
}
|
||||
|
||||
/*
|
||||
* Occupies the given board position for the player whose turn it is.
|
||||
*
|
||||
* This function does *not* check whether there is a winner first.
|
||||
* x and y must lie within the board's boundaries.
|
||||
*
|
||||
* Returns
|
||||
* -1 if the given position is already occupied, or
|
||||
* 0 on success.
|
||||
*/
|
||||
int tictactoe_game_make_turn(ttt_game_t *game, size_t x, size_t y)
|
||||
{
|
||||
if (game->board[x][y] != ' ') {
|
||||
if (game->board[y][x] != ' ') {
|
||||
pr_notice("position already occupied\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
game->board[x][y] = game->next_turn;
|
||||
game->board[y][x] = game->next_turn;
|
||||
game->winner = tictactoe_game_check_winner(game);
|
||||
|
||||
if (game->next_turn == 'X')
|
||||
@@ -110,9 +127,12 @@ int tictactoe_game_make_turn(ttt_game_t *game, size_t x, size_t y)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Prints a human-readable visualization of the game state to buf.
|
||||
*/
|
||||
int tictactoe_game_snprint(ttt_game_t *game, char *buf, size_t count)
|
||||
{
|
||||
int pos;
|
||||
size_t pos;
|
||||
int ret;
|
||||
|
||||
pos = 0;
|
||||
@@ -124,10 +144,10 @@ int tictactoe_game_snprint(ttt_game_t *game, char *buf, size_t count)
|
||||
goto fail;
|
||||
pos += ret;
|
||||
|
||||
if (game->winner == 0) {
|
||||
if (!game->winner) {
|
||||
ret = snprintf(buf + pos, count - pos, "It's %c's turn!\n",
|
||||
game->next_turn);
|
||||
} else if (game->winner == -1) {
|
||||
} else if (game->winner == '\xFF') {
|
||||
ret = snprintf(buf + pos, count - pos, "It's a tie!\n");
|
||||
} else {
|
||||
ret = snprintf(buf + pos, count - pos, "Player %c won!\n",
|
||||
|
||||
+2
-3
@@ -3,14 +3,13 @@
|
||||
|
||||
#include <linux/types.h>
|
||||
|
||||
typedef struct tictactoe_game {
|
||||
typedef struct {
|
||||
char next_turn;
|
||||
char winner;
|
||||
char board[3][3];
|
||||
} ttt_game_t;
|
||||
|
||||
int tictactoe_game_init(ttt_game_t *game);
|
||||
char tictactoe_game_check_winner(ttt_game_t *game);
|
||||
void tictactoe_game_init(ttt_game_t *game);
|
||||
int tictactoe_game_make_turn(ttt_game_t *game, size_t x, size_t y);
|
||||
int tictactoe_game_snprint(ttt_game_t *game, char *buf, size_t len);
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#define tictactoe_error(format, ...) \
|
||||
pr_err("%s: " format, __func__, ##__VA_ARGS__)
|
||||
|
||||
typedef struct annotated_string {
|
||||
typedef struct {
|
||||
struct mutex lock;
|
||||
size_t len;
|
||||
char *buf;
|
||||
|
||||
+9
-11
@@ -94,7 +94,7 @@ static ssize_t tictactoe_write(struct file *file, const char __user *buf,
|
||||
{
|
||||
char *command_buf;
|
||||
size_t count_without_lf;
|
||||
size_t human_x, human_y;
|
||||
size_t x, y;
|
||||
|
||||
if (count == 0)
|
||||
return 0;
|
||||
@@ -128,6 +128,8 @@ static ssize_t tictactoe_write(struct file *file, const char __user *buf,
|
||||
tictactoe_game_init(tictactoe_current_game);
|
||||
mutex_unlock(&tictactoe_mutex_current_game);
|
||||
kfree(command_buf);
|
||||
|
||||
*ppos += count;
|
||||
return count;
|
||||
}
|
||||
|
||||
@@ -145,8 +147,8 @@ static ssize_t tictactoe_write(struct file *file, const char __user *buf,
|
||||
}
|
||||
}
|
||||
|
||||
human_x = (size_t)(command_buf[0] - '0');
|
||||
human_y = (size_t)(command_buf[1] - '0');
|
||||
x = (size_t)(command_buf[0] - '1');
|
||||
y = (size_t)(command_buf[1] - '1');
|
||||
|
||||
kfree(command_buf);
|
||||
|
||||
@@ -163,13 +165,14 @@ static ssize_t tictactoe_write(struct file *file, const char __user *buf,
|
||||
#endif
|
||||
}
|
||||
|
||||
if (tictactoe_game_make_turn(tictactoe_current_game, human_x - 1,
|
||||
human_y - 1)) {
|
||||
if (tictactoe_game_make_turn(tictactoe_current_game, x, y)) {
|
||||
mutex_unlock(&tictactoe_mutex_current_game);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
mutex_unlock(&tictactoe_mutex_current_game);
|
||||
|
||||
*ppos += count;
|
||||
return count;
|
||||
}
|
||||
|
||||
@@ -226,12 +229,7 @@ static int __init tictactoe_init(void)
|
||||
tictactoe_error("failed to allocate game memory\n");
|
||||
return -EFAULT;
|
||||
}
|
||||
ret = tictactoe_game_init(tictactoe_current_game);
|
||||
if (ret) {
|
||||
mutex_unlock(&tictactoe_mutex_current_game);
|
||||
tictactoe_error("failed to initialize game\n");
|
||||
return -EFAULT;
|
||||
}
|
||||
tictactoe_game_init(tictactoe_current_game);
|
||||
mutex_unlock(&tictactoe_mutex_current_game);
|
||||
|
||||
ret = misc_register(&tictactoe_dev);
|
||||
|
||||
Reference in New Issue
Block a user