From dcddb96f934bc7a2a4d4160cc24955bbc5ac1bfe Mon Sep 17 00:00:00 2001 From: luxick Date: Fri, 25 Oct 2019 21:24:25 +0200 Subject: [PATCH] Initial --- libmttt.nimble | 13 +++++++++++++ src/libmttt.nim | 42 ++++++++++++++++++++++++++++++++++++++++++ tests/config.nims | 1 + tests/test1.nim | 15 +++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 libmttt.nimble create mode 100644 src/libmttt.nim create mode 100644 tests/config.nims create mode 100644 tests/test1.nim diff --git a/libmttt.nimble b/libmttt.nimble new file mode 100644 index 0000000..118ae80 --- /dev/null +++ b/libmttt.nimble @@ -0,0 +1,13 @@ +# Package + +version = "0.1.0" +author = "luxick" +description = "Game library for meta tic tac toe" +license = "GPL-2.0" +srcDir = "src" + + + +# Dependencies + +requires "nim >= 1.0.0" diff --git a/src/libmttt.nim b/src/libmttt.nim new file mode 100644 index 0000000..f4ef247 --- /dev/null +++ b/src/libmttt.nim @@ -0,0 +1,42 @@ +import strformat + +type + Board*[T] = + array[3, array[3, T]] + + Mark* = enum + mPlayer1, # The mark of the fist player + mPlayer2, # The mark of the second player + mFree # This mark signals that a cell is empty + + BoardResult* = enum + rPlayer1, # The first player has won the board + rPlayer2, # The second player has won the board + rDraw, # There is no winner. The board has ended in a draw + rOpen # The game on this board is still ongoing + +proc newBoard[T](initial: T): Board[T] = + result = [ + [initial, initial, initial], + [initial, initial, initial], + [initial, initial, initial] + ] + +proc newMetaBoard[T](val: T): Board[Board[T]] = + [ + [newBoard(val), newBoard(val), newBoard(val)], + [newBoard(val), newBoard(val), newBoard(val)], + [newBoard(val), newBoard(val), newBoard(val)] + ] + +proc createBoard*(): Board[Board[Mark]] = + newMetaBoard(mFree) + +proc checkBoard*(board: Board[Mark]): BoardResult = + for x in 0 ..< board.len: + for y in 0 ..< board.len: + echo fmt"Cell (x: {x}, y: {y}) = {board[x][y]}" + +proc checkBoard*(board: Board[Board[Mark]]): BoardResult = + rOpen + diff --git a/tests/config.nims b/tests/config.nims new file mode 100644 index 0000000..3bb69f8 --- /dev/null +++ b/tests/config.nims @@ -0,0 +1 @@ +switch("path", "$projectDir/../src") \ No newline at end of file diff --git a/tests/test1.nim b/tests/test1.nim new file mode 100644 index 0000000..06e536a --- /dev/null +++ b/tests/test1.nim @@ -0,0 +1,15 @@ +# This is just an example to get you started. You may wish to put all of your +# tests into a single file, or separate them into multiple `test1`, `test2` +# etc. files (better names are recommended, just make sure the name starts with +# the letter 't'). +# +# To run these tests, simply execute `nimble test`. + +import unittest + +import libmttt + +test "debugging": + var metaBoard = createBoard() + metaBoard[0][0][1][1] = mPlayer1 + echo metaBoard[0][0].checkBoard()