Initial
This commit is contained in:
13
libmttt.nimble
Normal file
13
libmttt.nimble
Normal file
@@ -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"
|
||||
42
src/libmttt.nim
Normal file
42
src/libmttt.nim
Normal file
@@ -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
|
||||
|
||||
1
tests/config.nims
Normal file
1
tests/config.nims
Normal file
@@ -0,0 +1 @@
|
||||
switch("path", "$projectDir/../src")
|
||||
15
tests/test1.nim
Normal file
15
tests/test1.nim
Normal file
@@ -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()
|
||||
Reference in New Issue
Block a user