-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UX #4
base: master
Are you sure you want to change the base?
UX #4
Changes from all commits
eb2d196
b31fc6a
648e8ef
09a6312
85576df
b57fdc8
65bc4ff
61775d7
114e7a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,8 +29,11 @@ | |
(it "should know if there is a draw" | ||
(should= true (draw? ["x"]))) | ||
|
||
(it "should know valid positions" | ||
(should= false (valid-position? ["x"] 0))) | ||
(it "should know if a position is valid" | ||
(should= true (valid-position? ["x" "-"] 1))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can use the function |
||
|
||
(it "should know if a position is not valid" | ||
(should= false (valid-position? ["x" "-"] 0))) | ||
|
||
(it "should know all available positions" | ||
(should= [1 2] (available-positions ["x" "-" "-"]))) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
(ns clojure-ttt.presenter-spec | ||
(:require [speclj.core :refer :all] | ||
[clojure-ttt.presenter :refer :all])) | ||
|
||
(describe "presenter" | ||
(it "displays gamemode options" | ||
(should-contain "Type 0 to play human vs human" | ||
(with-out-str (show-gamemode-options)))) | ||
|
||
(it "displays the board correctly" | ||
(should-contain "x--\nx--\n---" | ||
(with-out-str (show-board ["x" "-" "-" "x" "-" "-" "-" "-" "-"])))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should use keywords to represent marks, rather than strings. |
||
|
||
(it "displays the winner" | ||
(should-contain "x has won the game!" | ||
(with-out-str (show-round-message ["x" "x" "x" "o" "-" "-" "-" "o" "o"])))) | ||
|
||
(it "displays if its a draw" | ||
(should-contain "draw" | ||
(with-out-str (show-round-message ["x" "x" "o" | ||
"o" "x" "x" | ||
"x" "o" "o"]))))) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,45 @@ | ||
(ns clojure-ttt.game | ||
(use [clojure-ttt.board])) | ||
(use [clojure-ttt.board] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use |
||
[clojure-ttt.computer] | ||
[clojure-ttt.presenter])) | ||
|
||
(defn game-over? [board] | ||
(or (draw? board) (any-wins? board))) | ||
|
||
(defn- read-move [] | ||
(read-string (read-line))) | ||
|
||
(defn make-move [board] | ||
(defn make-move [board player] | ||
(let [move (read-move)] | ||
(cond | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an |
||
(valid-position? board move) (mark-board board move (find-turn board)) | ||
(valid-position? board move) (mark-board board move player) | ||
:else board))) | ||
|
||
(defn start-game [board] | ||
(println board) | ||
(defn make-move-computer [board player] | ||
(mark-board board (find-best-move board player) player)) | ||
|
||
(defn play-until-over [board gamemode] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where are the tests for this new functionality? |
||
(show-round-message board) | ||
(show-board board) | ||
(when-not (game-over? board) | ||
(recur (make-move board)))) | ||
(let [turn (find-turn board)] | ||
(cond | ||
(and (= gamemode :hvc) (= turn "o")) (recur (make-move-computer board turn) gamemode) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
(= gamemode :cvc) (recur (make-move-computer board turn) gamemode) | ||
:else (recur (make-move board turn) gamemode))))) | ||
|
||
(defn- ask-for-settings [board] | ||
(show-gamemode-options) | ||
(let [input (read-move)] | ||
(cond | ||
(= input 1) (play-until-over board :hvc) | ||
(= input 2) (play-until-over board :cvc) | ||
:else (play-until-over board :hvh)))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about invalid entries? Where should the responsibility for validation go? |
||
|
||
(defn ask-to-play-again [] | ||
(show-options) | ||
(if (= 1 (read-move)) (ask-for-settings (create-board 3)))) | ||
|
||
(defn -main [& args] | ||
(start-game (create-board 3))) | ||
(ask-for-settings (create-board 3)) | ||
(ask-to-play-again)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
(ns clojure-ttt.presenter | ||
(use [clojure-ttt.board])) | ||
|
||
(defn- clear-console [] | ||
(println "\033[H\033[2J")) | ||
|
||
(defn- welcome-user [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no spec for this |
||
(println "\033[35mWelcome! Please select a gamemode.") | ||
(println "------------------------------------------")) | ||
|
||
(defn show-gamemode-options [] | ||
(clear-console) | ||
(welcome-user) | ||
(println "\033[36mType 0 to play human vs human. \n1 to play vs a computer. \n2 to watch two computers play.")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was not clear when displayed in the console. Probably because of the word 'type' being either a verb or a noun.. You could lose 'type' altogether and have: |
||
|
||
(defn show-board [board] | ||
(let [partitioned-board (map #(apply str %) (partition 3 board))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
(print "\033[31m") | ||
(println (apply str(interpose "\n" partitioned-board))))) | ||
|
||
(defn show-round-message [board] | ||
(println (str "\033[35m" "--------------------------")) | ||
(cond | ||
(any-wins? board) (println (str (last-move board) " has won the game!")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These |
||
(draw? board) (println "The game is a draw!") | ||
:else (println (str (find-turn board) " please make your move.")))) | ||
|
||
(defn show-options [] | ||
(println "Press 1 to play again, anything else to quit.")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You use the words
should know
a lot, I would either simplify that toknows
ordetermines
e.g.
(it 'determines if a position is valid')