mirror of
https://github.com/nelsbrock/dev-tictactoe.git
synced 2026-08-14 20:56:49 +02:00
Compare commits
6
Commits
4520ba5b61
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
610bc1c07c
|
||
|
|
12eba69eb1
|
||
|
|
2b55717595
|
||
|
|
504c950c15
|
||
|
|
ebaf64982d
|
||
|
|
ac4d4eb4fa
|
@@ -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 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 #
|
||||
# #
|
||||
#####
|
||||
|
||||
It's X's turn!
|
||||
|
||||
+7
-8
@@ -7,7 +7,7 @@
|
||||
/*
|
||||
* Initializes a new game instance.
|
||||
*/
|
||||
int tictactoe_game_init(ttt_game_t *game)
|
||||
void tictactoe_game_init(ttt_game_t *game)
|
||||
{
|
||||
game->next_turn = 'X';
|
||||
game->winner = 0;
|
||||
@@ -16,18 +16,17 @@ int tictactoe_game_init(ttt_game_t *game)
|
||||
game->board[x][y] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Steps over the board, starting from position (start_x, start_y) and ending
|
||||
* 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 lie within the board's boundaries.
|
||||
* start_x and start_y must be within the board's boundaries.
|
||||
*
|
||||
* Returns the respective player, or '\x00' if a cell isn't occupied.
|
||||
* 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,
|
||||
@@ -112,12 +111,12 @@ static char tictactoe_game_check_winner(ttt_game_t *game)
|
||||
*/
|
||||
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')
|
||||
|
||||
+2
-2
@@ -3,13 +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);
|
||||
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