Compare commits

...
4 Commits
5 changed files with 15 additions and 18 deletions
+4 -4
View File
@@ -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: (ranging from 1 to 3, format `XY`) to `/dev/tictactoe` to make their first move:
```console ```console
# echo 12 > /dev/tictactoe # echo 22 > /dev/tictactoe
``` ```
After player X has made their move, player O can do the same: After player X has made their move, player O can do the same:
```console ```console
# echo 22 > /dev/tictactoe # echo 13 > /dev/tictactoe
``` ```
To inspect the current state of the game, simply read from `/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! It's X's turn!
+3 -5
View File
@@ -7,7 +7,7 @@
/* /*
* Initializes a new game instance. * 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->next_turn = 'X';
game->winner = 0; game->winner = 0;
@@ -16,8 +16,6 @@ int tictactoe_game_init(ttt_game_t *game)
game->board[x][y] = ' '; game->board[x][y] = ' ';
} }
} }
return 0;
} }
/* /*
@@ -113,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) 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"); pr_notice("position already occupied\n");
return -1; return -1;
} }
game->board[x][y] = game->next_turn; game->board[y][x] = game->next_turn;
game->winner = tictactoe_game_check_winner(game); game->winner = tictactoe_game_check_winner(game);
if (game->next_turn == 'X') if (game->next_turn == 'X')
+2 -2
View File
@@ -3,13 +3,13 @@
#include <linux/types.h> #include <linux/types.h>
typedef struct tictactoe_game { typedef struct {
char next_turn; char next_turn;
char winner; char winner;
char board[3][3]; char board[3][3];
} ttt_game_t; } 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_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); int tictactoe_game_snprint(ttt_game_t *game, char *buf, size_t len);
+1 -1
View File
@@ -10,7 +10,7 @@
#define tictactoe_error(format, ...) \ #define tictactoe_error(format, ...) \
pr_err("%s: " format, __func__, ##__VA_ARGS__) pr_err("%s: " format, __func__, ##__VA_ARGS__)
typedef struct annotated_string { typedef struct {
struct mutex lock; struct mutex lock;
size_t len; size_t len;
char *buf; char *buf;
+5 -6
View File
@@ -128,6 +128,8 @@ static ssize_t tictactoe_write(struct file *file, const char __user *buf,
tictactoe_game_init(tictactoe_current_game); tictactoe_game_init(tictactoe_current_game);
mutex_unlock(&tictactoe_mutex_current_game); mutex_unlock(&tictactoe_mutex_current_game);
kfree(command_buf); kfree(command_buf);
*ppos += count;
return count; return count;
} }
@@ -169,6 +171,8 @@ static ssize_t tictactoe_write(struct file *file, const char __user *buf,
} }
mutex_unlock(&tictactoe_mutex_current_game); mutex_unlock(&tictactoe_mutex_current_game);
*ppos += count;
return count; return count;
} }
@@ -225,12 +229,7 @@ static int __init tictactoe_init(void)
tictactoe_error("failed to allocate game memory\n"); tictactoe_error("failed to allocate game memory\n");
return -EFAULT; return -EFAULT;
} }
ret = tictactoe_game_init(tictactoe_current_game); tictactoe_game_init(tictactoe_current_game);
if (ret) {
mutex_unlock(&tictactoe_mutex_current_game);
tictactoe_error("failed to initialize game\n");
return -EFAULT;
}
mutex_unlock(&tictactoe_mutex_current_game); mutex_unlock(&tictactoe_mutex_current_game);
ret = misc_register(&tictactoe_dev); ret = misc_register(&tictactoe_dev);