Last active
March 24, 2021 21:56
-
-
Save fennecdjay/fafb414c6449a14c07537d49e098b83d to your computer and use it in GitHub Desktop.
the code for tictactoe in gwion. The file ends in `.c` instead of `.gw` to get syntax highlight
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#require TUI | |
#require Std | |
const global Event ev; | |
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]] => const int wins[][]; | |
class TicTacToeBoard extends TUI.User { | |
var int current; | |
"X" => var string player; | |
var int count; #! there can be only 9 moves | |
var bool do_reset; | |
var string board[9]; | |
fun void reset() { | |
for(var int i; i < board.size(); i++) { | |
" " => board[i]; | |
} | |
} | |
reset(); | |
(9, 6) => size; | |
callback('q', \a{ ev.signal(); }); | |
fun void draw_playable(const int i, const int row) { | |
write("${board[i]} | ${board[i+1]} | ${board[i+2]}", 0, row); | |
markup(TUI.attribute(TUI.Color.reset), 0, row, 9); | |
} | |
fun void draw_fence(const int row) { | |
write("--+---+--", 0, row); | |
markup(TUI.attribute(TUI.Color.reset), 0, row, 9); | |
} | |
#! "X": Xs won | |
#! "O": Os won | |
#! " ": Tie | |
#! "": Keep playing | |
fun string game_state() { | |
foreach (win: wins) { | |
if (board[win[0]] == board[win[1]] | |
&& board[win[0]] == board[win[2]] | |
&& board[win[0]] != " ") { | |
return board[win[0]]; | |
} | |
} | |
if (count == 9) | |
return " "; | |
return ""; | |
} | |
fun void draw() { | |
board[current] == " " => const bool mark; | |
draw_playable(0, 0); | |
draw_fence(1); | |
draw_playable(3, 2); | |
draw_fence(3); | |
draw_playable(6, 4); | |
game_state() => const string state; | |
#! These spaces are necessary to pad the text to 12 long so that | |
#! we don't see remnants of the old text since it doesn't overwrite | |
#! all of it. | |
if(state == "") { | |
write("You are: ${player}s ", 0, 5); | |
} else if (state == " ") { | |
write("It's a draw!", 0, 5); | |
#! true => do_reset; | |
0 => count; | |
} else { | |
write("${state}s won! ", 0, 5); | |
true => do_reset; | |
0 => count; | |
} | |
current / 3 => const int row; | |
current % 3 => const int col; | |
if (mark) { | |
write("*", col * 8, row * 2); | |
} | |
markup(TUI.attribute(TUI.Color.red), col * 8, row * 2, 1); | |
} | |
fun void activate(int keycode, TUI.Intent intent) { | |
if(count == 9) { | |
#! ev.signal(); | |
return; | |
} | |
if(do_reset) { | |
reset(); | |
false => do_reset; | |
return; | |
} | |
match intent { | |
case TUI.Next: | |
if (++current >= 9) | |
0 => current; | |
case TUI.Previous:; | |
if (--current < 0) | |
8 => current; | |
case TUI.Activate: | |
if (board[current] == " ") { | |
count++; | |
player => board[current]; | |
if (player == "X") | |
"O" => player; | |
else | |
"X" => player; | |
} | |
} | |
} | |
fun void select(int keycode, TUI.Intent intent) {} | |
} | |
const TUI.Window win; | |
" Tic Tac Toe" => win.title; | |
win.size(14, 11); | |
const TicTacToeBoard board; | |
const TUI.Button reset_button; | |
"Reset Board" => reset_button.text; | |
fun void reset(const TicTacToeBoard board) { | |
board.reset(); | |
} | |
#!reset_button.callback('\n', \a {board.reset();} ); | |
win << board | |
<< reset_button; | |
ev => now; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment