How to resolve the algorithm Flipping bits game step by step in the Clojure programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Flipping bits game step by step in the Clojure programming language
Table of Contents
Problem Statement
Given an N×N square array of zeroes or ones in an initial configuration, and a target configuration of zeroes and ones.
The game is to transform one to the other in as few moves as possible by inverting whole numbered rows or whole lettered columns at once (as one move). In an inversion. any 1 becomes 0, and any 0 becomes 1 for that whole row or column.
Create a program to score for the Flipping bits game.
Show an example of a short game here, on this page, for a 3×3 array of bits.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Flipping bits game step by step in the Clojure programming language
Source code in the clojure programming language
(defn cols [board]
(mapv vec (apply map list board)))
(defn flipv [v]
(mapv #(if (> % 0) 0 1) v))
(defn flip-row [board n]
(assoc board n (flipv (get board n))))
(defn flip-col [board n]
(cols (flip-row (cols board) n)))
(defn play-rand [board n]
(if (= n 0)
board
(let [f (if (= (rand-int 2) 0) flip-row flip-col)]
(recur (f board (rand-int (count board))) (dec n)))))
(defn rand-binary-vec [size]
(vec (take size (repeatedly #(rand-int 2)))))
(defn rand-binary-board [size]
(vec (take size (repeatedly #(rand-binary-vec size)))))
(defn numbers->letters [coll]
(map #(char (+ 97 %)) coll))
(defn column-labels [size]
(apply str (interpose " " (numbers->letters (range size)))))
(defn print-board [board]
(let [size (count board)]
(println "\t " (column-labels size))
(dotimes [n size] (println (inc n) "\t" (board n)))))
(defn key->move [key]
(let [start (int (first key))
row-value (try (Long/valueOf key) (catch NumberFormatException e))]
(cond
(<= 97 start 122) [:col (- start 97)]
(<= 65 start 90) [:col (- start 65)]
(> row-value 0) [:row (dec row-value)]
:else nil)))
(defn play-game [target-board current-board n]
(println "\nTurn " n)
(print-board current-board)
(if (= target-board current-board)
(println "You win!")
(let [move (key->move (read-line))
axis (first move)
idx (second move)]
(cond
(= axis :row) (play-game target-board (flip-row current-board idx) (inc n))
(= axis :col) (play-game target-board (flip-col current-board idx) (inc n))
:else (println "Quitting!")))))
(defn -main
"Flip the Bits Game!"
[& args]
(if-not (empty? args)
(let [target-board (rand-binary-board (Long/valueOf (first args)))]
(println "Target")
(print-board target-board)
(play-game target-board (play-rand target-board 3) 0))))
You may also check:How to resolve the algorithm Selectively replace multiple instances of a character within a string step by step in the Perl programming language
You may also check:How to resolve the algorithm Return multiple values step by step in the Déjà Vu programming language
You may also check:How to resolve the algorithm Address of a variable step by step in the Quackery programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the Unicon programming language
You may also check:How to resolve the algorithm Nth root step by step in the Dart programming language