Download - Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Transcript
Page 1: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Project 1Tic Tac Toe

Robb T. Koether

Hampden-Sydney College

Fri, Sep 9, 2011

Page 2: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Outline

1 Introduction

2 Drawing the Game Board

3 Handling Resizing Events

4 Handling Mouse Events

5 The Game Logic

6 Tic Tac Toe Functions

7 Strategy

Page 3: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Outline

1 Introduction

2 Drawing the Game Board

3 Handling Resizing Events

4 Handling Mouse Events

5 The Game Logic

6 Tic Tac Toe Functions

7 Strategy

Page 4: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Introduction

• In this project you will write a program that displays a tictac toe board and lets the user play the game.

• I have provided all the tic-tac-toe logic.• You will access that logic through a set of functions.

Page 5: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

The User Interface

• The user presses X to start a new game where he goesfirst.

• He presses O to start a new game where the computergoes first.

• The user clicks in the cell where he wants to move.• He presses ESC when he wants to quit.

Page 6: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Your Job

Your job is to• Provide the graphics for

• The game grid.• The X’s.• The O’s.• The winning line.

• Handle mouse-clicks in the window.• Handle keystrokes.• Handle resizing events.

Page 7: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Outline

1 Introduction

2 Drawing the Game Board

3 Handling Resizing Events

4 Handling Mouse Events

5 The Game Logic

6 Tic Tac Toe Functions

7 Strategy

Page 8: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Drawing the Game Grid

• The game grid is the traditional 3× 3 board.• It consists of two horizontal bands and two vertical

bands on a dark green background (0, 128, 0).

Page 9: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Drawing the Game Grid

• The 9 empty squares formed by the grid should havelength and width equal to CELL_WIDTH.

• The thickness of the grid lines should be equal toGRID_WIDTH.

GRID_WIDTH

CELL_WIDTH

(0,0)

Page 10: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Drawing an X

• The X should be drawn as a polygons.• The corners should be shaped as shown in the

diagram.

Page 11: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Drawing an X

• The diagonals should be drawn at 45◦ angles.• The thickness of the X should be equal toGRID_WIDTH.

GRID_WIDTH

Page 12: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Drawing an O

• The O should be circular.• The circle will actually be one many-sided polygon

inside another.

Page 13: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Drawing an O

• The thickness of the O should be GRID_WIDTH.

GRID_WIDTH

Page 14: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

The Cell Padding

• The space between the X or the O and the grid linesshould be equal to CELL_PADDING.

CELL_PADDING

CELL_PADDING

Page 15: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Drawing the Winning Lines

• Horizontal and vertical winning lines are rectangles withlength equal to the size of the game grid and widthequal to GRID_WIDTH.

Page 16: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Drawing the Winning Lines

• The diagonal winning lines are similar to the diagonalsof the X, but they extend from one corner of the gamegrid to the other.

Page 17: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Drawing Polygons

• Keep in mind that OpenGL assumes that all polygonsare convex.

• If you try to draw non-convex polygons, the results areunpredictable.

• The parameters CELL_WIDTH, GRID_WIDTH, andCELL_PADDING should be built in as constants.

• I suggest that you create a new constantBOARD_WIDTH that is derived from the others.

• The GL_POLYGON type has been deprecated.• Use the GL_TRIANGLE_FAN type instead.

Page 18: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Outline

1 Introduction

2 Drawing the Game Board

3 Handling Resizing Events

4 Handling Mouse Events

5 The Game Logic

6 Tic Tac Toe Functions

7 Strategy

Page 19: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Handling Resizing Events

• The game board should always be centered in thewindow.

• It will grow or shrink with the window.• The game board is square.• If the window is wider than it is tall, then the game

board will occupy the middle 80% vertically.• If the window is taller than it is wide, then the game

board will occupy the middle 80% horizontally.

Page 20: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Handling Resize Events

Page 21: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Handling Resize Events

Page 22: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Handling Resize Events

x

y

(0, 0)

BOAR

D_WI

DTH

(height − BOARD_WIDTH)/2

(height − BOARD_WIDTH)/2ymin

ymax

Determine ymin and ymax when aspect ratio > 1

Page 23: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Handling Resize Events

x

y

(0, 0)

BOARD_WIDTH(w

idth

− BOA

RD_W

IDTH

)/2

xmin ymax

(wid

th −

BOA

RD_W

IDTH

)/2

Determine xmin and xmax when aspect ratio > 1

Page 24: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Outline

1 Introduction

2 Drawing the Game Board

3 Handling Resizing Events

4 Handling Mouse Events

5 The Game Logic

6 Tic Tac Toe Functions

7 Strategy

Page 25: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Handling Mouse-Clicks

• The cells are numbered 0 through 8.• Cells 0, 1, 2 are in the bottom row, left to right.• Cells 3, 4, 5 are in the middle row and cells 6, 7, 8 are

in the top row.• I will provide the function nextMove() that will return

the position of the computer’s next move.• You should then draw an O in the cell that it returns.

Page 26: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Handling Mouse-Clicks

• Let a represent the width of a cell and b represent thewidth of a grid line.

• Let x be the x-coordinate of the mouse click.• Then c = x ÷ (a + b) will give the column number of the

click (0, 1, or 2).

a b a b ax

y

Page 27: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Handling Mouse-Clicks

• Let t = x mod (a + b). If 0 ≤ t ≤ a, then the click wasin the cell.

a b a b ax

y

Page 28: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Handling Mouse-Clicks

• If t > a, then the click was in the grid line.

a b a b ax

y

Page 29: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Handling Mouse-Clicks

• In a similar way, we can get the row number r (0, 1, or2).

• If we know the column number c and the row number r ,how can we compute the cell number n?

• Conversely, if we know the cell number n (0, 1, 2, . . . ,8), how can we compute the row and column numbersr and c?

Page 30: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Outline

1 Introduction

2 Drawing the Game Board

3 Handling Resizing Events

4 Handling Mouse Events

5 The Game Logic

6 Tic Tac Toe Functions

7 Strategy

Page 31: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

The Game Logic

• There will be 5 levels of play, activated by pressing thekeys F1 through F5, respectively.

• The default is level 5.

Page 32: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

The Game Logic–Level 1

Level 1 Logic

• Make a random move.

Page 33: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

The Game Logic–Level 2

Level 2 Logic

• If the player has a winning move, take it.• Otherwise, make a random move.

Page 34: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

The Game Logic–Level 3

Level 3 Logic

• If the player has a winning move, take it.• If the player has no winning move, but the opponent

has a winning move, block it.• Otherwise, make a random move.

Page 35: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

The Game Logic–Level 4

Level 4 Logic

• If the player has a winning move, take it.• If the player has no winning move, but the opponent

has a winning move, block it.• If neither player has a winning move, but the player has

a move that will create a winning move, take it.• Otherwise, make a random move.

Page 36: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

The Game Logic–Level 5

Level 5 Logic

• If the player has a winning move, take it.• If the player has no winning move, but the opponent

has a winning move, block it.• If neither player has a winning move, but the player has

a move that will create a winning move, take it.• If neither player has a winning move and the player

does not have a move that will create a winning move,but the opponent does have a move that will create awinning move, block it.

• Otherwise, make a random move.

Page 37: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Outline

1 Introduction

2 Drawing the Game Board

3 Handling Resizing Events

4 Handling Mouse Events

5 The Game Logic

6 Tic Tac Toe Functions

7 Strategy

Page 38: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Tic Tac Toe Files

• You will be given the files• TTTLogic.h• TTTLogic.cpp• set.h

• The file TTTLogic.h contains the statementtypedef Set<9> Player;

Page 39: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

The Player Objects

• You will need two global variables of type Player.• You might name them Xmoves and Omoves.• Xmoves contains the set of cells that X occupies.• Omoves contains the set of cells that O occupies.

Page 40: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Player Member Functions

Player Member Functions

• Xmoves.isMember(cell) - returns true if X occupiesthat cell.

• Xmoves.makeEmpty() - clear all of the X’s moves.• Xmoves.size() - returns the cells occupied by X.• Xmoves[i] - X’s i-th move (ordered by position).

Page 41: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Player Operators

Operator +=

• Xmoves += cell;

• Add cell to X’s moves.

Page 42: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Tic Tac Toe Functions

Tic Tac Toe Functions

• int nextMove(Player p1, Player p2, intlevel);

• bool isOccupied(Player p, int cell);

• nextMove() - Returns the next move (cell) for playerp2.

• isOccupied() - Returns true if the cell is occupiedby player p.

Page 43: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Tic Tac Toe Functions

Tic Tac Toe Functions

• bool isGameOver(Player p1, Player p2);

• bool hasWon(Player p);

• int winningConfig(Player p);

• isGameOver() - Returns true if the game is over.• hasWon() - Returns true if player p has won.• winningConfig() - Returns -1 if player p has not

won; returns winning configuration (see next slide) if phas won.

Page 44: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Winning Configurations

• There are 8 winning configurations.• Configurations 0, 1, 2 - rows 0, 1, 2, respectively.• Configurations 3, 4, 5 - columns 0, 1, 2, respectively.• Configuration 6 - diagonal from upper left to lower right.• Configuration 7 - diagonal from lower left to upper right.

0

2

1

3 547

6

Page 45: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Outline

1 Introduction

2 Drawing the Game Board

3 Handling Resizing Events

4 Handling Mouse Events

5 The Game Logic

6 Tic Tac Toe Functions

7 Strategy

Page 46: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Divide and Conquer

• Do not try to write the entire program before testing anyof it.

• Write one part that can be tested before moving on tothe next part.

Page 47: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Divide and Conquer

• I have provided a file namedDivideAndConquer.cpp that contains some of thecode you will need.

• I suggest that you• First get the big square to be centered when the

window is resized.• Then get the mouse-clicks to draw the red square

wherever you click.• Then get draw the red square only if the mouse-clicks

are within the big square.• Then replace the red square with your X, then your O.• And so on.

• At each stage, get it working before going on the thenext stage.

Page 48: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Divide and Conquer

• I have provided a file namedDivideAndConquer.cpp that contains some of thecode you will need.

• I suggest that you• First get the big square to be centered when the

window is resized.• Then get the mouse-clicks to draw the red square

wherever you click.• Then get draw the red square only if the mouse-clicks

are within the big square.• Then replace the red square with your X, then your O.• And so on.

• At each stage, get it working before going on the thenext stage.

Page 49: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Divide and Conquer

• I have provided a file namedDivideAndConquer.cpp that contains some of thecode you will need.

• I suggest that you• First get the big square to be centered when the

window is resized.• Then get the mouse-clicks to draw the red square

wherever you click.• Then get draw the red square only if the mouse-clicks

are within the big square.• Then replace the red square with your X, then your O.• And so on.

• At each stage, get it working before going on the thenext stage.

Page 50: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Divide and Conquer

• I have provided a file namedDivideAndConquer.cpp that contains some of thecode you will need.

• I suggest that you• First get the big square to be centered when the

window is resized.• Then get the mouse-clicks to draw the red square

wherever you click.• Then get draw the red square only if the mouse-clicks

are within the big square.• Then replace the red square with your X, then your O.• And so on.

• At each stage, get it working before going on the thenext stage.

Page 51: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Divide and Conquer

• I have provided a file namedDivideAndConquer.cpp that contains some of thecode you will need.

• I suggest that you• First get the big square to be centered when the

window is resized.• Then get the mouse-clicks to draw the red square

wherever you click.• Then get draw the red square only if the mouse-clicks

are within the big square.• Then replace the red square with your X, then your O.• And so on.

• At each stage, get it working before going on the thenext stage.

Page 52: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Divide and Conquer

• I have provided a file namedDivideAndConquer.cpp that contains some of thecode you will need.

• I suggest that you• First get the big square to be centered when the

window is resized.• Then get the mouse-clicks to draw the red square

wherever you click.• Then get draw the red square only if the mouse-clicks

are within the big square.• Then replace the red square with your X, then your O.• And so on.

• At each stage, get it working before going on the thenext stage.

Page 53: Project 1 - Tic Tac Toe - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms331/Lectures... · Tic Tac Toe Functions Strategy Introduction In this project you will write

Project 1

Robb T.Koether

Introduction

Drawing theGame Board

HandlingResizingEvents

HandlingMouse Events

The GameLogic

Tic Tac ToeFunctions

Strategy

Divide and Conquer

• I have provided a file namedDivideAndConquer.cpp that contains some of thecode you will need.

• I suggest that you• First get the big square to be centered when the

window is resized.• Then get the mouse-clicks to draw the red square

wherever you click.• Then get draw the red square only if the mouse-clicks

are within the big square.• Then replace the red square with your X, then your O.• And so on.

• At each stage, get it working before going on the thenext stage.