# CS 1571: Homework 5

## Game-Playing (Chapter 6): Paper (50 pts) and Programming (150 pts)

Assigned: October 7, 2008

Paper Due: October 16, 2008

Programming Due: October 21, 2008

#### Adversarial Search (50 pts)

Consider the A*Search problem from a prior homework with a slight alteration. Consider a sliding block puzzle with the following initial configuration:
WWEBB
1 2 3 4 5
There are two white checkers belonging to player 1, two black checkers belonging to player 2, and an empty cell. The game ends when a player moves all of his checkers to the opposite side of the board. Player 1 moves first. If player 1 wins, 1 point is awarded to player 1 and -1 points are awarded to player 2 and vice versa. The legal moves for a player are:

- Move a tile to an adjacent empty cell
- Hop over one or two other tiles into an empty cell

  If a player cannot make a move, the turn is forfeited and the other player may make another move. Player 1 can only move to the right and player 2 can only move to the left.

  (a) Draw the complete game tree with the following conventions:

- Represent each state as a pair (x1x2, y1y2), where each xi is a position with a white checker and each yi is a position with a black checker (with the numbers used in the diagram above). Label the edges of the game tree with the player to makes the move.
- Put terminal states in a thick, square box with the utility function value for player 1 inside.

  (b) Now label each node with its backed-up minimax value for player 1.

  (c) Give a solution path that rational players would play.

  #### Generalized tic-tac-toe (150 pts)

  The purpose of this problem is to give you experience with minimax search with alpha beta pruning, and with developing heuristics.

  Your task is to write a program to play tic-tac-toe which meets the specifications below. Standard tic-tac-toe, also called noughts and crosses, hugs and kisses, and many other names, is a pen-and-paper game for two players, X and O, who take turns to mark the spaces in a 3 by 3 grid. The player who succeeds in placing three of their own marks in a horizontal, vertical or diagonal row wins the game.

  For this assignment, we will consider a generalized k/2-in-a-row on an k x k board variation of tic-tac-toe. (If k/2 yields a fraction, round down.)

  Chenhai will run all of your programs so that each of you plays a tic-tac-toe player that picks an open square to place a mark in. See if you can win!

  To make playing this player possible, your submission must satisfy the following (exactly!):

- Include a "playerA" program that reads the board parameter k from stdin, and the game board from file board.txt.
- The program computes and prints the X player's move to standard output (it's always X's turn to play).
- Include a Makefile that produces an executable file called "playerA"
- Make sure that your Makefile and executable run on javalab.cs.pitt.edu or on unixs.cis.pitt.edu (and specify which).

  The file board.txt contains only a row-major representation of the board. For example, X___O____ represents a 3x3 board with 'X' in its (0,0) i.e., top right position, 'O' in its center position (1,1). An empty position is indicated by an underscore '_'. A move by your player must be in the form of a single integer between zero and (k*k)-1 (inclusive). The move has to be valid in the sense that it represents an empty board position.

  Note that while ideally a player would search the complete game tree, you probably do not have the computational resources. To address this, your program should implement a limited-depth exploration of the game tree, where nodes at a depth limit are evaluated using a board evaluation heuristic. We gave an example tic-tac-toe heuristic in one of the classes. Note that the heuristic function is used to evaluate the leaves of the (depth-limited) tree only. You can choose the depth limit to optimize your player, BUT if your limit causes your program to take more than a minute per step, we will end the game and your player will be declared the loser.

  In addition to your code, your submission should contain a description of your heuristic. You should also play at least 10 matches. To illustrate that your program works correctly, please show a trace for one of the matches. To illustrate the quality of your player, report the number of wins, draws, and losses for the 10 matches.

  To evaluate your program, we will also run a script that repeatedly invokes your player and respond to its moves by (1) updating board.txt with player X's move, (2) determining if player X has won, (2) and if not, updating board.txt with player O's move. Your player will always go first.

  Your assignment will be graded on:

- Correctness
- Quality of play (how often your program beats Chenhai's player)

  Software to help you

  The support code written for this assignment lets you play two different programs against each other. You can find the code here. Alternatively, you can get it on javalab at

- /afs/cs.pitt.edu/usr0/chenhai/public/html/CS1571/tic-tac-toe

  There is a Readme file which describes all files. In particular, you will play a game using driver.pl, which iteratively takes two players, playerA and playerB. For your assignment, you will be writing code to replace "playerA".

  Chenhai's C source code player.c shows how to read k from the standard input, and how to output the move to standard output. Right now driver.pl outputs the whole process to the standard error, feel free to modify this to redirect your output to a file.