Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const std = @import("std");
- const warn = std.debug.warn;
- var board = [_]u8{'_'} ** 9;
- fn askUserInput() !void {
- const stdin = std.io.getStdIn().inStream();
- while (true) {
- warn("Your turn, choose your move:\n", .{});
- const player_move = try stdin.readIntNative(u8);
- if (player_move >= '1' and player_move <= '9') {
- const board_field = player_move - '0' - 1;
- if (board[board_field] != '_') {
- const cell_value = [1:0]u8{board[board_field]};
- warn("Wrong board position, this cell is already {}\n", .{cell_value});
- } else {
- board[board_field] = 'O';
- return;
- }
- } else {
- warn("Wrong number, please write number from 1 to 9\n", .{});
- }
- }
- }
- pub fn main() !void {
- // Pressing "a" and then Enter, which is invalid input renders this:
- //
- // Wrong number, please write number from 1 to 9
- // Your turn, choose your move:
- // Wrong number, please write number from 1 to 9
- // Your turn, choose your move:
- //
- // Why does it render these messages twice? Pressing Enter
- // without "a" renders it just once.
- try askUserInput();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement