License Public Domain
Lines 150
Keywords
example (2) F# (1) game (1)
Included in this Library
Permissions
Group Owner: .Net
Viewable by Everyone
Editable by Spencer Ruport and ".Net" Group Administrators
Hide
Easily highlight source code for your blog with our Syntax Highlighter. Join Siafoo Now or Learn More

TicTacToe in F# Atom Feed 0

In Brief This was my first attempt at an F# application. I haven't messed with a functional language in several years so my technique is probably abysmal. Still, I'm pleased with the result.
# 's
  1#light
2open System
3
4let rec ValidInput =
5 fun msg ->
6 fun valid ->
7 printf "%s" msg
8 let chr = Console.ReadKey().KeyChar.ToString()
9 printfn ""
10 let exists = List.exists (fun x -> x = chr) valid
11 if exists then chr
12 else
13 printfn "Invalid entry."
14 ValidInput msg valid
15
16let rec BoardIsFull : list<string> -> int -> bool =
17 fun board ->
18 fun index ->
19 if (List.nth board index) = " " then
20 false
21 else if index = (board.Length - 1) then
22 true
23 else
24 BoardIsFull board (index + 1)
25
26let rec GetStraightWins : list<string> -> int -> string =
27 fun straight ->
28 fun index ->
29 if index = straight.Length then
30 List.nth straight 0
31 else if (List.nth straight index) = " " then
32 ""
33 else if (List.nth straight 0) <> (List.nth straight index) then
34 ""
35 else
36 GetStraightWins straight (index + 1)
37
38let rec GetWinner : list<string> -> int -> string =
39 fun straightwins ->
40 fun index ->
41 if index = straightwins.Length then
42 ""
43 else if (List.nth straightwins index) <> "" then
44 List.nth straightwins index
45 else
46 GetWinner straightwins (index + 1)
47
48let Winner : list<string> -> int -> string =
49 fun board ->
50 fun size ->
51 let rows =
52 [for x in 0 .. (size - 1) ->
53 [for y in 0 .. (size - 1) ->
54 List.nth board (x * size + y)
55 ]
56 ]
57 let cols =
58 [for x in 0 .. (size - 1) ->
59 [for y in 0 .. (size - 1) ->
60 List.nth board (y * size + x)
61 ]
62 ]
63 let diagonals =
64 [for x in 0 .. 1 ->
65 [for y in 0 .. (size - 1) ->
66 if x = 0 then
67 List.nth board (y * size + y)
68 else
69 List.nth board ((size - 1 - y) * size + y)
70 ]
71 ]
72 let straights = List.append (List.append rows cols) diagonals
73// printf "Straights: "
74// for x in 0 .. (straights.Length - 1) do
75// let temp = (List.nth straights x)
76// printf "[ "
77// for y in 0 .. (temp.Length - 1) do
78// printf "%s " (List.nth temp y)
79// done
80// printfn "]"
81// done
82// printfn ""
83
84
85 let straightwins = List.map (fun straight -> GetStraightWins straight 0) straights
86// printf "Winners: "
87// for x in 0 .. (winners.Length - 1) do
88// printf "[ "
89// printf "%s " (List.nth winners x)
90// printfn "]"
91// done
92// printfn ""
93 GetWinner straightwins 0
94
95let PrintBoard : list<string> -> int -> unit =
96 fun board ->
97 fun size ->
98 for index = 0 to (board.Length - 1) do
99 printf "%s" (List.nth board index)
100 if (index + 1) % size = 0 then
101 printfn ""
102 if index <> (board.Length - 1) then
103 for i = 1 to (size * 2 - 1) do
104 if i % 2 = 0 then
105 printf "+"
106 else
107 printf "-"
108 done
109 printfn ""
110 else
111 printf "|"
112 done
113
114let rec TicTacToe =
115 fun board ->
116 fun turn ->
117 fun size ->
118 let WinningPlayer = (Winner board size)
119 PrintBoard board size
120 if WinningPlayer <> "" then
121 printfn "%s wins!" WinningPlayer
122 else if BoardIsFull board 0 then
123 printfn "Cats game!"
124 else
125 if turn = 0 then
126 printfn "Os turn:"
127 else
128 printfn "Xs turn:"
129 let range = [for x in 1 .. size -> x.ToString()]
130 let row = System.Int32.Parse(ValidInput "Please select a row: " range) - 1
131 let col = System.Int32.Parse(ValidInput "Please select a column: " range) - 1
132 Console.Clear()
133 if (List.nth board (row * size + col)) <> " " then
134 printfn "That space has already been taken."
135 TicTacToe board turn size
136 else
137 TicTacToe [for index in 0 .. board.Length - 1 ->
138 if index = (row * size + col) then
139 if turn = 0 then
140 "O"
141 else
142 "X"
143 else
144 List.nth board index
145 ] (-(turn - 1)) size
146
147let main =
148 printf "Please enter the size of the board: "
149 let size = System.Int32.Parse(Console.ReadKey().KeyChar.ToString())
150 Console.Clear()
151 let board = [for x in 1 .. (size * size) -> " "]
152 TicTacToe board 0 size
153 printfn ""
154 let ret = Console.ReadKey().KeyChar
155 printfn ""
156
157main

This was my first attempt at an F# application. I haven't messed with a functional language in several years so my technique is probably abysmal. Still, I'm pleased with the result.