CSC 1520 Computer Principles - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

CSC 1520 Computer Principles

Description:

Hints: Winner check. cin inputRow; cin inputCol; //sanity ... Hints: Program Flow. Initialize board. BLACK goes. LOOP. WHITE goes. WHITE goes. BLACK goes ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 33
Provided by: hcw9
Category:

less

Transcript and Presenter's Notes

Title: CSC 1520 Computer Principles


1
CSC 1520 Computer Principles C Programming
  • Assignment 5
  • - Yangfan

2
Assignment 5connect6???
Due 235959, 19 Nov 2008 (Wed)
3
Assignment 5
  • Task Implement the game Connect6
  • The game is VERY similar to Connect4 in
    assignment 4
  • Purpose of this assignment
  • Practice more on functions and arrays

4
  • Game rules of connect6

5
Game Rules
  • Like Connect4, Connect 6 is also a two-player
    game
  • One player is called BLACK
  • Another is called WHITE

6
Game Rules
  • Game board Like GO (??), the game board is a
    1919 board

(18,18)
Coordinates the same as connect4 --- (row,
column)
0-18
19 rows
0-18
19 columns
(0,0)
7
Game Rules
  • The placing rule is different from Connect4.
  • Connect6 stones can be placed anywhere
  • Connect4 discs must drop down to the bottom of a
    column

8
Game Rules
  • BLACK moves first by placing one black stone on
    the board

(16,15)
9
Game Rules
  • Subsequently, WHITE and BLACK alternately put two
    of their own stones on two unoccupied sites
  • ?????????

10
Game Rules
  • Horizontal winning condition

Winner BLACK
Horizontally 6 stones are connected
11
Game Rules
  • Vertical winning conditions

Winner WHITE
Vertically 6 stones are connected
12
Game Rules
  • Diagonal winning conditions

Winner WHITE
Diagonally 6 stones are connected
13
Game Rules
  • Diagonal winning conditions

Winner WHITE
Diagonally 6 stones are connected
14
Game Rules
  • Winning conditions are almost the same as the
    connect4 games in assignment 4
  • Except that you should consider 6 stones rather
    than 4 discs

15
Game Rules
  • If the board is full and no one has connected six
    stones ? DRAW
  • Exactly the same as Connect4 in assignment 4

16
Game Rules
  • Game is OVER when
  • Player 1 or player 2 wins, or
  • The game results in draw.
  • Exactly the same as Connect4 in assignment 4

17
  • How to implement Connect6

18
Game Flow and Display Things
  • Game begins with an empty board.
  • Every site is initialized with a dot .

????
\t
19
Game Flow
  • BLACK goes first
  • Ask the BLACK to input a choice of where to put a
    BLACK stone

Here shows who is the current player.
Here prompts for user input (two integer numbers
which are the row and column of where the stone
shall be put
20
Game Flow
  • WHITE goes
  • Ask the WHITE to input a choice of where to put a
    WHITE stone

Like assignment 4, X denotes the stone of BLACK
Here shows who is the current player.
Here prompts for user input (two integer numbers
which are the row and column of where the stone
shall be put
21
Game Flow
  • Then WHITE goes again
  • Ask the WHITE to input a choice of where to put a
    WHITE stone

Like assignment 4, _at_ denotes the stone of WHITE
Here prompts for user input (two integer numbers
which are the row and column of where the stone
shall be put
22
Game Flow
  • Then BLACK.

Both player put two stones after the BLACKs
first move
23
Sanity Check
  • Every time when the current player inputs the row
    and column, you should check if the site (row,
    column) has already been occupied. If YES
  • Warn the player
  • Prompt the player to enter another (row, column)

24
Sanity Check
  • Every time when the current player inputs the row
    and column, you should check if the row and
    column inputted are in 0-18 inclusively. If NO
  • Warn the player
  • Prompt the player to enter another (row, column)

25
Data Structure
  • A game board must be represented by a
    2-dimentional array

const int ROW 19 const int COL 19 char
boardROWCOL //board122 //is the site at
row 12 and column 2 //hints //board122
_at_ // the site is occupied by
WHITE //board122 X // the site is
occupied by BLACK //board122 .
the site is empty
26
Game Flow
  • Program terminates when the game is over
  • Either player wins (you should check vertical,
    horizontal, and diagonal winning conditions)
  • Unlike Connect4, checking diagonal winning
    condition is mandatory in this assignment
  • The board is full
  • Display a winning or draw message like

When BLACK wins cout the winner!
27
Game Flow
  • How to check if a player wins
  • You must design a function declared as
  • bool hasWon(char boardCOL, int row, int
    col)
  • The function assumes that the position (row, col)
    of the game board is occupied (i.e., not .).
  • It should return true if boardrowcol has
    connected six stones horizontally, vertically, or
    diagonally and false otherwise.
  • This function should be called right after a
    player has placed a stone at (row, col).
  • If it returns true, then the player that has
    placed a stone at (row, col) wins

28
Hints Winner check
//. cin inputRow cin inputCol //sanity
check. if pass //boardinputRowinputCol X
/ _at_ depending on who is the current player if
(hasWon(board, inputRow, inputCol)) if
(boardinputRowinputCol X) nWinner
BLACK else nWinner WHITE //Now
we can output who wins and terminate the
program else // continue the game
29
Game Flow
  • How to check if it results in draw game
  • You can use a counter to count how many stones
    are put. If the number is 361, game draws.
  • Or, you can use loops to check whether there is a
    . in boardROWCOL. If yes, game has not
    drawn yet.

30
Hints Program Flow
  • Initialize board
  • BLACK goes
  • LOOP
  • WHITE goes
  • WHITE goes
  • BLACK goes
  • BLACK goes
  • Print board
  • Show result
  • Print board
  • ASK WHITE to input
  • Sanity check (false re-input)
  • Update board
  • Winner check (true break the loop)
  • Draw check (true break the loop)


  • Print board
  • ASK BLACK to input
  • Sanity check (false re-input)
  • Update board
  • Winner check (true break the loop)
  • Draw check (true break the loop)

31
Important Notes
  • Program should be reasonably decomposed into at
    least four functions (including main and hasWon).
    You may consider to decompose the program as
    follows
  • Initialization of an empty game board
  • User input and sanity check
  • Output of the game board
  • hasWon (compulsory)
  • main (compulsory)
  • Do NOT try to use cf_board.h or cf_board.obj in
    assignment 4, they are particularly designed for
    assignment 4

32
Any Questions?Thank you!Feel free to email me
if you have any problems.
Write a Comment
User Comments (0)
About PowerShow.com