12 pontoon1may 15 12 pontoon program ce00858-1: fundamental programming techniques

12
12 Pontoon 1 19 Jul 2022 12 Pontoon program CE00858-1: Fundamental Programming Techniques

Upload: griffin-mcbride

Post on 17-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 12 Pontoon1May 15 12 Pontoon program CE00858-1: Fundamental Programming Techniques

12 Pontoon 118 Apr 2023

12 Pontoon program

CE00858-1: Fundamental Programming Techniques

Page 2: 12 Pontoon1May 15 12 Pontoon program CE00858-1: Fundamental Programming Techniques

12 Pontoon 218 Apr 2023

Objectives

In this session, we will:• analyse a problem that uses methods • implement the solution using concepts introduced so

far

Page 3: 12 Pontoon1May 15 12 Pontoon program CE00858-1: Fundamental Programming Techniques

12 Pontoon

Pontoon specification

• a simple game based upon the card game Pontoon• player will play the game against the computer• cards are allocated numeric values

• Ace = 11, Jack = 10, Queen = 10, King = 10• although in Pontoon, Ace can be either 11 or 1, we will only

use 11• Cards 2 -10 have their face value

• player and computer are dealt two cards in turn initially then can take additional cards• player takes all additional cards before computer• computer must take additional cards if score less than 15

• score over 21 "busts"• winner has highest score that is 21 or under• computer's score beats player's if equal

18 Apr 2023 3

Page 4: 12 Pontoon1May 15 12 Pontoon program CE00858-1: Fundamental Programming Techniques

12 Pontoon

Analysis – break down

• relatively large problem• break down into smaller chunks• analyse, implement and test each chunk:

• deal initial hands• deal additional cards to player• check if player bust• deal additional cards to computer• check if computer bust • check for winner

• also need to consider how to represent cards

18 Apr 2023 4

Page 5: 12 Pontoon1May 15 12 Pontoon program CE00858-1: Fundamental Programming Techniques

12 Pontoon

//play a game of pontoonimport java.util.*;

public class Pontoon{

public static void main (String args[]){

//deal initial hands//deal additional cards to player//deal additional cards to computer//check if anyone bust//check for winner if no-one bust

}}

18 Apr 2023 5

Page 6: 12 Pontoon1May 15 12 Pontoon program CE00858-1: Fundamental Programming Techniques

Representing cards

• each card has a rank and an integer score

• not concerned with suit of card• value is randomly generated• need to:

• output rank based on value• determine score based on value

12 Pontoon 618 Apr 2023

Value Rank Score

1 Ace 11

2 – 10 2 – 10 face value

11 Jack 10

12 Queen 10

13 King 10

Page 7: 12 Pontoon1May 15 12 Pontoon program CE00858-1: Fundamental Programming Techniques

12 Pontoon

Testing: Deal initial hands

• add output statements to display scores after each card dealt

• run program and ensure:• 2 random card values generated for player• 2 random card values generated for computer• rank displayed correctly• scores calculated correctly

18 Apr 2023 7

Page 8: 12 Pontoon1May 15 12 Pontoon program CE00858-1: Fundamental Programming Techniques

12 Pontoon

Testing: Deal additional cards to player

• run program and ensure:• user asked whether they want new card after initial

hand dealt• loop entered when user first types 'y'• loop continued when user next types 'y' • loop terminated when user types anything else

18 Apr 2023 8

Page 9: 12 Pontoon1May 15 12 Pontoon program CE00858-1: Fundamental Programming Techniques

12 Pontoon

Testing: Deal additional cards to computer

• add statement to set computerScore to 0 before loop

• run and ensure loop is entered at least twice• change statement to set computerScore to 14 • run and ensure loop is entered when score is

14 (boundary case)• change statement to set computerScore to 15• run and ensure loop is not entered when score

is 15 (boundary case)• remove added statement

18 Apr 2023 9

Page 10: 12 Pontoon1May 15 12 Pontoon program CE00858-1: Fundamental Programming Techniques

12 Pontoon

Testing: Check if anyone bust

• add statement to set player’s score to 21 before checking to see if anyone bust

• run program and ensure player doesn’t bust• change statement to set player’s score to 22• run program and ensure that player busts• remove statement

• add statement to set computer's score to 21 before checking to see if anyone bust

• run program and ensure computer doesn’t bust• change statement to set computer's score to 22• run program and ensure that computer busts• remove statement

18 Apr 2023 10

Page 11: 12 Pontoon1May 15 12 Pontoon program CE00858-1: Fundamental Programming Techniques

12 Pontoon

Testing: Check win

• add statements after checking if anyone bust to set player’s score and computer’s score so that player's score is higher than computer's

• run program and ensure that player wins• change statements so computer's score and

player's score are equal• run program and ensure that computer wins• change statements so computer's score is

higher than player's score• run program and ensure that computer wins• remove added statements

18 Apr 2023 11

Page 12: 12 Pontoon1May 15 12 Pontoon program CE00858-1: Fundamental Programming Techniques

12 Pontoon 1218 Apr 2023

Summary

In this session we have:• analysing a fairly large problem to determine what is

required• implementing a solution that:

• generates random numbers• uses while loops• uses if statements • uses methods to implement parts of the solution

In the next session we will:• use arrays to store data