Skip to content

Commit

Permalink
Merge pull request #10 from titouanfreville/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
titouanfreville authored Jun 17, 2017
2 parents 688251e + 9fdfe1f commit 7b5e84b
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 47 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ This project aims to implement a playing interface for the square game. It is a

Download the binary corresponding to your system:

- Linux, Ubuntu architecture: [download Linux 64bits executable](https://github.com/titouanfreville/squaregame_ia/raw/dev/src/square-game)
- MacOS: [download mac 64bits executable]()
- Windows: [download windows 64bits executable]()
- Linux, Ubuntu architecture: [download Linux 64bits executable](https://github.com/titouanfreville/squaregame_ia/raw/master/build/linux/square-game)
- MacOS: [download mac 64bits executable](https://github.com/titouanfreville/squaregame_ia/raw/master/build/mac/square-game)
- Windows: [download windows 64bits executable](https://github.com/titouanfreville/squaregame_ia/raw/master/build/windows/square-game)

### From source

Expand Down
Binary file modified build/linux/square-game
Binary file not shown.
39 changes: 26 additions & 13 deletions src/graphic/grid.ml
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,36 @@ let rec updateNthElement l n e =
| ((t::q),0) -> e::q
| (t::q, _) -> t::updateNthElement q (n-1) e;;

let rec isClosedCase lbool =
match lbool with
| [] -> true
| t::q -> t && isClosedCase q

let rec addPlay grid i j side =
let ((_,_), lbool, lnode) = searchNode grid i j in
let (ni,nj) = getNthElement lnode side and played = getNthElement lbool side in
if played
then
grid
else
let newLBoll = updateNthElement lbool side true in
let newGrid = updateNode grid i j ((i,j), newLBoll, lnode) in
if ni = -99
then
newGrid
else
addPlay newGrid ni nj ((side+2) mod 4)
;;
try
let (ni,nj) = getNthElement lnode side and played = getNthElement lbool side in
if played
then
(grid, false, false)
else
let newLBool = updateNthElement lbool side true in
let newGrid = updateNode grid i j ((i,j), newLBool, lnode) in
let closed = isClosedCase newLBool in
if ni = -99
then
(newGrid, true, closed)
else
let (g, _, nclosed) = addPlay newGrid ni nj ((side+2) mod 4) in
let closed = nclosed || closed in
(g, true, closed)
with
NotEnougthElementInList -> (EmptyGrid, false, false)

let rec allClosed grid =
match grid with
| EmptyGrid -> true
| GridNode(ng, (_,lbool,_), nd) -> isClosedCase lbool && allClosed ng && allClosed nd

(*
@initGridRec
Expand Down
3 changes: 2 additions & 1 deletion src/graphic/grid.mli
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ val initGridRec : int -> int -> int -> int -> gridNode grid
val initGrid : int -> int -> gridNode grid
val searchNode : gridNode grid -> int -> char -> gridNode
val updateNode : gridNode grid -> int -> char -> gridNode -> gridNode grid
val addPlay : gridNode grid -> int -> char -> int -> gridNode grid
val addPlay : gridNode grid -> int -> char -> int -> gridNode grid * bool * bool
val allClosed : gridNode grid -> bool
(* List general functions *)
val getNthElement : 'a list -> int -> 'a
val updateNthElement : 'a list -> int -> 'a -> 'a list
Expand Down
116 changes: 86 additions & 30 deletions src/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,34 @@ let readChar () =
let r = read_line () in
r.[0]

let getVal valName valType getter=
print_string ("Enter "^valName^": "^valType^" <- ");
getter ()

let printCurrentGrid grid =
Grid.magicDrawing grid Grid.printNode 200 1000

let printCurrentScore p1 p2 s1 s2 =
print_string "<><><><><><><><><><><><><><><><><><><><><><><><><><>\n";
print_string "<><><><><><><><><><><> SCORE! <><><><><><><><><><><>\n";
print_string ("<><> Player "^p1^": "); print_int s1; print_string ".\n";
print_string ("<><> Player "^p2^": "); print_int s2; print_string ".\n";
print_string "<><><><><><><><><><><><><><><><><><><><><><><><><><>\n"

let printWinner p s =
print_string ("\nWELL PLAY "^p^" !!!!!\nYou won this game with ");
print_int s; print_string (" points.\n")

let printResult p1 p2 s1 s2 =
if s1 = s2
then print_string ("\nIT'S A TIE !!!!!\nWell play both: "^p1^" and "^p2^".\n")
else
if s1 > s2
then
printWinner p1 s1
else
printWinner p2 s2

(* Ia calls *)
let playNoob grid = grid;;

Expand All @@ -18,21 +43,37 @@ let playIa1 = playNoob;;
let playIa2 = playNoob;;
(* Player plays *)

let rec move grid =
let rec move grid score =
print_string ("Enter your move (x: int, y: char, side: int): \n");
let i = read_int () and j = readChar () and side = read_int () in
Grid.addPlay grid i j side
let i = getVal "x" "int" read_int and j = getVal "y" "char" readChar and side = getVal "side" "int in [0,1,2,3]" read_int in
let (g, ok, replay) = Grid.addPlay grid i j side in
if ok
then
begin
if replay
then
if allClosed g
then (g, score+1)
else move g (score+1)
else (g, score)
end
else
begin
print_string ("The move you enter can't be proced. Ever it was already play, ever it is outside the grid. Please try again !\n");
move grid score
end

let reset () =
print_string ("Enter the size of the grid you wish (x:int, y:int):\n");
let x = read_int () and y = read_int () in
let x = getVal "x" "int" read_int and y = getVal "y" "int" read_int in
let grid = initGrid x y in
Grid.resetGraph();
printCurrentGrid grid;
grid
(grid, -2)

let endGame () =
Grid.EmptyGrid
let endGame name =
print_string ("player "^name^" leaved the game.\n");
(Grid.EmptyGrid, -1)

(*
@waitPlayer
Expand All @@ -41,15 +82,15 @@ let endGame () =
@param grid (gridNode grid) game grid
@return (gridNode grid) new grid according to user play or choice
*)
let rec waitPlayer name grid =
print_string ("Playing: "^name);
let rec waitPlayer name grid score =
print_string ("Playing: "^name^"\n");
print_string ("What do you want to do ?\nPlay a Move (play)\nReset the game (reset)\nLeave the game (leave):\n");
let userinput = read_line() in
match userinput with
| "play" -> move grid
| "play" -> move grid score
| "reset" -> reset ()
| "leave" -> endGame ()
| _ -> waitPlayer name grid
| "leave" -> endGame name
| _ -> waitPlayer name grid score

(* Player creation*)
(*
Expand All @@ -60,30 +101,45 @@ let rec waitPlayer name grid =
@param grid (gridNode grid)
@return grid
*)
let player name kind grid =
let player name kind score grid =
match kind with
| "human" | "h" -> waitPlayer name grid
| "ia0" | "0" -> playIa0 grid
| "ia1" | "1" -> playIa1 grid
| _ -> playIa2 grid

let rec game grid p1 p2 t1 t2 =
let ngrid = player p1 t1 grid in
match ngrid with
| Grid.EmptyGrid -> exit 0
| _ -> printCurrentGrid ngrid;
let ngrid = player p2 t2 ngrid in
match ngrid with
| Grid.EmptyGrid -> exit 0
| _ -> printCurrentGrid ngrid; game ngrid p1 p2 t1 t2;;
| "human" | "h" -> waitPlayer name grid score
| "ia0" | "0" -> (playIa0 grid, score)
| "ia1" | "1" -> (playIa1 grid, score)
| _ -> (playIa2 grid, score)

let rec game grid p1 p2 t1 t2 s1 s2 =
if allClosed grid
then (printResult p1 p2 s1 s2; exit 0;);
let (ngrid, ns1) = player p1 t1 s1 grid in
if ns1 = -2
then game ngrid p1 p2 t1 t2 0 0
else
match ngrid with
| Grid.EmptyGrid -> exit 0
| _ -> printCurrentGrid ngrid; printCurrentScore p1 p2 ns1 s2;
if allClosed ngrid
then (printResult p1 p2 s1 s2; exit 0;);
let (ngrid, ns2) = player p2 t2 s2 ngrid in
if ns2 = -2
then game ngrid p1 p2 t1 t2 0 0
else
match ngrid with
| Grid.EmptyGrid -> exit 0
| _ -> printCurrentGrid ngrid; printCurrentScore p1 p2 ns1 ns2; game ngrid p1 p2 t1 t2 ns1 ns2;;


(* Main block *)
let main () =
Grid.openGraph "1900" "800";
let n1 = "me" and n2 = "maya" in
let grid = reset () in
game grid n1 n2 "h" "0";;

let (grid, s) = reset ()
and n1 = getVal "player 1" "string" read_line
and t1 = getVal "type of Player 1" "string in [h,0,1,2]" read_line
and n2 = getVal "player 2" "string" read_line
and t2 = getVal "type of Player 2" "string in [h,0,1,2]" read_line in

game grid n1 n2 t1 t2 0 0;;

(* Running main *)
main ()

0 comments on commit 7b5e84b

Please sign in to comment.