C S 335 Project Strategy Design Pattern

Collaboration: Solo
Due Monday 4-Sep 19:00, 50 points, Turnin to D2L Dropbox "Strategy"

Given the beginning of a game of Tic Tac Toe game, add these two TicTacToeStrategy types that can be swapped at runtime.

  1. Beginner: choose any available square at random, which should be easy to beat
  2. Intermediate: Choose any available square at random unless the human player is about to win, then block that square. If there are 2 ways for the human player to win, choose either square.

Begin with this Java project that has a useless strategy OnlyChose0_0 that picks row 0, column 0 once and then shows a message every other time it is called. TicTacToeStart.zip Here is a UML class diagram of the current system plus the two required classes that must be made to implement TicTacToeStrategy: Beginner and Intermediate

If you select the menu items to set the strategy to Beginner or Intermediate, you will get null pointer exceptions since both listeners set the ComputerPlayer's stategy to null. In the meantime, only one move can be made. Run Tournament.java in the tests package to get this output (and a message)

ComputerPlayer beginner = new ComputerPlayer();
beginner.setStrategy(new OnlyChoose0_0());
ComputerPlayer intermediate = new ComputerPlayer();
intermediate.setStrategy(new OnlyChoose0_0());
Game g = new Game();

// Pass a reference to the current game so the ComputerPlayer

// strategy can look at the game to decide which square to chose
beginner.chooseSquareOn(g);
System.out.println(g.toString());
// Currently cannot play ComputerPlayer against ComputerPlayer
intermediate.chooseSquareOn(g);
System.out.println(g.toString());

Grading Criteria 50 points max (Subject to change)

+2 class Beginner implements TicTacToeStrategy

+2 class Intermediate implements TicTacToeStrategy

+10 The Beginner Strategy chooses any available square at random

+4 We can always beat the Beginner Strategy

+16 The Intermediate strategy blocks a square that would have allow the human to win had she chosen it next.

+4 We can almost always beat the Intermediate Strategy

+12 You have a Java application (a class with a main method) in the test folder that pits the two strategies together and show the Intermediate wins more often than the ComputerPlayer with the Beginner strategy. Run 1000s game twice and print results where each strategy goes first. Output should look like this:

Result of playing 1000 games when Beginner goes first:
Beginner 125
Ties 315
Intermediate 560
Result of playing 1000 games when Intermediate goes first:
Beginner 24
Ties 103
Intermediate 873