Question

In: Computer Science

Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your...

Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your program must use min-max search and should be invoked by the function call:

> (Tic-Tac-Toe)

The game is single player, human vs the computer AI.

Solutions

Expert Solution

(defun generate-board ()

(loop repeat 9 collect nil))

(defparameter *straights* '((1 2 3) (4 5 6) (7 8 9) (1 4 7) (2 5 8) (3 6 9) (1 5 9) (3 5 7)))

(defparameter *current-player* 'x)

(defun get-board-elt (n board)

(nth (1- n) board))

(defun legal-p (n board)

(null (get-board-elt n board)))

(defun set-board-elt (n board symbol)

(if (legal-p n board)

(setf (nth (1- n) board) symbol)

(progn (format t "Illegal move. Try again.~&")

(set-board-elt (read) board symbol))))

(defun list-legal-moves (board)

(loop for i from 1 to (length board)

when (legal-p i board)

collect i))

(defun get-random-element (lst)

(nth (random (length lst)) lst))

(defun multi-non-nil-eq (lst)

(and (notany #'null lst)

(notany #'null (mapcar #'(lambda (x) (eq (car lst) x)) lst))

(car lst)))

(defun elements-of-straights (board)

(loop for i in *straights*

collect (loop for j from 0 to 2

collect (get-board-elt (nth j i) board))))

(defun find-winner (board)

(car (remove-if #'null (mapcar #'multi-non-nil-eq (elements-of-straights board)))))

(defun set-player (mark)

(format t "Shall a computer play as ~a? (y/n)~&" mark)

(let ((response (read)))

(cond ((equalp response 'y) t)

((equalp response 'n) nil)

(t (format t "Come again?~&")

(set-player mark)))))

(defun player-move (board symbol)

(format t "~%Player ~a, please input your move.~&" symbol)

(set-board-elt (read) board symbol)

(format t "~%"))

(defun computer-move (board symbol)

(let ((move (get-random-element (list-legal-moves board))))

(set-board-elt move board symbol)

(format t "~%computer selects ~a~%~%" move)))

(defun computer-move-p (current-player autoplay-x-p autoplay-o-p)

(if (eq current-player 'x)

autoplay-x-p

autoplay-o-p))

(defun perform-turn (current-player board autoplay-x-p autoplay-o-p)

(if (computer-move-p current-player autoplay-x-p autoplay-o-p)

(computer-move board current-player)

(player-move board current-player)))

(defun switch-player ()

(if (eq *current-player* 'x)

(setf *current-player* 'o)

(setf *current-player* 'x)))

(defun display-board (board)

(loop for i downfrom 2 to 0

do (loop for j from 1 to 3

initially (format t "|")

do (format t "~a|" (or (get-board-elt (+ (* 3 i) j) board) (+ (* 3 i) j)))

finally (format t "~&"))))

(defun tic-tac-toe ()

(setf *current-player* 'x)

(let ((board (generate-board))

(autoplay-x-p (set-player 'x))

(autoplay-o-p (set-player 'o)))

(format t "~%")

(loop until (or (find-winner board) (null (list-legal-moves board)))

do (display-board board)   

do (perform-turn *current-player* board autoplay-x-p autoplay-o-p)

do (switch-player)

finally (if (find-winner board)

(format t "The winner is ~a!" (find-winner board))

(format t "It's a tie.")))))

Output:

CL-USER> (tic-tac-toe)

Shall a computer play as X? (y/n)

n

Shall a computer play as O? (y/n)

y

|7|8|9|

|4|5|6|

|1|2|3|

Player X, please input your move.

5

|7|8|9|

|4|X|6|

|1|2|3|

computer selects 8

|7|O|9|

|4|X|6|

|1|2|3|

Player X, please input your move


Related Solutions

Write a LISP program to play either the game "Connect Three" on a size 4x4 game...
Write a LISP program to play either the game "Connect Three" on a size 4x4 game board. Your program must use min-max search and should be invoked by the function call: > (connect-3) The game is single player, human vs the computer AI.
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
make a 4x4 tic tac toe in javascript (X is the user) (O is the computer)
make a 4x4 tic tac toe in javascript (X is the user) (O is the computer)
Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A...
Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the tic-tac-toe board. This game should involve one human player vs....
Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A...
Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the tic-tac-toe board. This game should involve one human player vs....
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out...
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out the state of the board, asks the user where they would like to place their mark, and implements this decision. The program then places its own mark on a randomly chosen available position. Once one of the player won, the program declares the result and asks if the user would like to continue. The first player is selected at random.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT