COSC 2425- Program Set #1
Chegwidden/Blackjack.cpp
Chegwidden/Blackjack.cpp
// Author: James Chegwidden
// Course: COSC 2436
// Program Set 1
// References: None
#include
<
iostream
>
#include
<
cstdlib
>
#include
<
ctime
>
using
namespace
std
;
const
bool
PLAYER_SIDE
=
true
;
const
bool
HOUSE_SIDE
=
false
;
// Functions shared by both sides
int
drawOneCard
();
int
addCardToHand
(
int
theHand
,
int
theCard
,
bool
&
handContainsAce11
);
int
drawAndAddCardToHand
(
int
theHand
,
bool
&
hasBigAce
);
// Output functions, sending messages to the user
void
announceCard
(
int
theCard
);
void
announceHand
(
int
theHand
);
void
announceResult
(
bool
side
,
int
theHand
);
void
whoWins
(
int
playerHand
,
int
houseHand
);
// Functions involved in building the player's hand
bool
isPlayerHolding
(
int
theHand
);
int
buildPlayerHand
();
// Functions involved in building the house's hand
int
buildHouseHand
(
int
theHand
,
bool
&
hasBigAce
);
int
drawOpenHouseCard
(
bool
&
hasBigAce
);
// The main program
int
main
()
{
// Welcome message and initializations
srand
(
static_cast
<
unsigned
>
(
time
(
NULL
)));
cout
<<
endl
<<
endl
;
cout
<<
"Welcome to the game of Blackjack!"
<<
endl
;
cout
<<
"---------------------------------"
<<
endl
<<
endl
;
// Show the house's open card
bool
houseHasBigAce
=
false
;
int
houseHand
=
drawOpenHouseCard
(
houseHasBigAce
);
// Then, build the player's hand
int
playerHand
=
buildPlayerHand
();
announceResult
(
PLAYER_SIDE
,
playerHand
);
// Unless the player has busted, build the house's hand
if
(
playerHand
<=
21
)
{
houseHand
=
buildHouseHand
(
houseHand
,
houseHasBigAce
);
announceResult
(
HOUSE_SIDE
,
houseHand
);
}
// Inform the player of the final result
whoWins
(
playerHand
,
houseHand
);
return
0
;
}
/********************************************************
OUTPUT FUNCTIONS
********************************************************/
// announceCard: What is the face value of the last card
// that was dealt?
void
announceCard
(
int
theCard
)
{
cout
<<
"--> "
;
switch
(
theCard
)
{
case
1
:
cout
<<
"An ace! "
;
break
;
case
2
:
cout
<<
"A deuce! "
;
break
;
case
3
:
cout
<<
"A three! "
;
break
;
case
4
:
cout
<<
"A four! "
;
break
;
case
5
:
cout
<<
"A five! "
;
break
;
case
6
:
cout
<<
"A six! "
;
break
;
case
7
:
cout
<<
"A seven! "
;
break
;
case
8
:
cout
<<
"An eight! "
;
break
;
case
9
:
cout
<<
"A nine! "
;
break
;
case
10
:
cout
<<
"A ten! "
;
break
;
case
11
:
cout
<<
"A jack! "
;
break
;
case
12
:
cout
<<
"A queen! "
;
break
;
case
13
:
cout
<<
"A king! "
;
break
;
default
:
cout
<<
"INVALID CARD!!!"
<<
endl
;
break
;
}
return
;
}
// announceHand: What is the current value of the hand?
void
announceHand
(
int
theHand
)
{
cout
<<
"The hand's value is now "
<<
theHand
<<
"."
<<
endl
;
return
;
}
// whoWins: tell the player who wins, based on the
// values of both hands at the end of the play
void
whoWins
(
int
playerHand
,
int
houseHand
)
{
cout
<<
endl
;
if
(
playerHand
>
21
)
cout
<<
"The house wins the hand."
<<
endl
;
else
if
(
houseHand
>
21
)
cout
<<
"The player wins the hand."
<<
endl
;
else
if
(
houseHand
>
playerHand
)
cout
<<
"The house wins the hand."
<<
endl
;
else
if
(
houseHand
<
playerHand
)
cout
<<
"The player wins the hand."
<<
endl
;
else
cout
<<
"The hand is a draw."
<<
endl
;
}
// announceResult: What is the final value of
// a given side's hand?
void
announceResult
(
bool
side
,
int
theHand
)
{
// Has the side in question busted? Yes, if its
// hand's value is over 21
if
(
theHand
>
21
)
{
if
(
HOUSE_SIDE
==
side
)
cout
<<
"The house busts!"
<<
endl
;
else
cout
<<
"The player busts!"
<<
endl
;
}
// Otherwise, the side has held
else
{
if
(
HOUSE_SIDE
==
side
)
cout
<<
"The house holds on "
<<
theHand
<<
endl
;
else
cout
<<
"The player holds on "
<<
theHand
<<
endl
;
}
return
;
}
/********************************************************
HOUSE HAND FUNCTIONS
The functions that help us build the house's hand
*******************************************************/
// buildHouseHand: Starting with a face card that has
// already been selected, draw cards until the house
// has at least 17
int
buildHouseHand
(
int
theHand
,
bool
&
hasBigAce
)
{
while
(
theHand
<
17
)
{
cout
<<
"The house draws on "
<<
theHand
<<
" and receives... "
<<
endl
;
theHand
=
drawAndAddCardToHand
(
theHand
,
hasBigAce
);
}
return
theHand
;
}
// drawOpenHouseCard: Draw a face card for the house
// before the player build his hand.
int
drawOpenHouseCard
(
bool
&
hasBigAce
)
{
cout
<<
"The dealer will draw an open card for the house..."
<<
endl
;
return
(
drawAndAddCardToHand
(
0
,
hasBigAce
)
);
}
/********************************************************
COMMON FUNCTIONS
These functions help build the hands of both player and
house
********************************************************/
// drawAndAddCardToHand: deal a card to the current side
int
drawAndAddCardToHand
(
int
theHand
,
bool
&
hasBigAce
)
{
int
theCard
=
drawOneCard
();
announceCard
(
theCard
);
theHand
=
addCardToHand
(
theHand
,
theCard
,
hasBigAce
);
announceHand
(
theHand
);
return
theHand
;
}
// drawOneCard: Pick a card at random; we assume an infinite
// supply of cards
int
drawOneCard
()
{
return
(
rand
()
%
13
+
1
);
}
// addCardToHand: Add the new card to the side's hand. If the
// new card makes the hand's total value higher than 21, see if
// the hand doesn't contain an ace whose value we can lower from
// 11 to 1.
int
addCardToHand
(
int
theHand
,
int
theCard
,
bool
&
handContainsAce11
)
{
// Add the nominal value; face cards are all worth 10 points
if
(
theCard
>
10
)
theHand
+=
10
;
// Aces are worth 11 if that is possible without busting,
// 1 otherwise
else
if
(
1
==
theCard
&&
theHand
<=
10
)
{
theHand
+=
11
;
handContainsAce11
=
true
;
}
// In all other cases, the card's face value is added
else
theHand
+=
theCard
;
// If the new hand value is over 21, try to shrink it
if
(
theHand
>
21
&&
handContainsAce11
)
{
theHand
-=
10
;
handContainsAce11
=
false
;
}
return
theHand
;
}
/*********************************************************
PLAYER FUNCTIONS
Functions that assist in assembling the human player's
hand
********************************************************/
// isPlayerHolding: Ask the player whether to hold or
// draw another card, based on his hand's current value
bool
isPlayerHolding
(
int
theHand
)
{
cout
<<
"Do you wish to hold on "
<<
theHand
<<
"? Type [Y]es or [N]o."
<<
endl
;
// Ask for answers until one is entered...
char
answer
;
do
{
cin
>>
answer
;
answer
=
toupper
(
answer
);
}
while
(
answer
!=
'Y'
&&
answer
!=
'N'
);
// Player holds on 'Y'es
return
(
'Y'
==
answer
);
}
// buildPlayerHand: Draw cards for the player until the
// player holds or busts
int
buildPlayerHand
()
{
// start with an empty hand
int
theHand
=
0
;
bool
hasBigAce
=
false
;
cout
<<
endl
<<
"The dealer will now deal cards to the player..."
<<
endl
;
// Give the player two cards to begin...
for
(
int
i
=
0
;
i
<
2
;
i
++
)
{
theHand
=
drawAndAddCardToHand
(
theHand
,
hasBigAce
);
}
// Then add one card at a time until the player holds or busts
bool
playerHolds
=
false
;
while
(
theHand
<
21
&&
!
playerHolds
)
{
playerHolds
=
isPlayerHolding
(
theHand
);
if
(
!
playerHolds
)
{
theHand
=
drawAndAddCardToHand
(
theHand
,
hasBigAce
);
}
}
return
theHand
;
}
Chegwidden/BlackjackRun.docx
image1.png
Chegwidden/NauticalMiles.cpp
Chegwidden/NauticalMiles.cpp
// Author: James Chegwidden
// Course: COSC 1437
// Program 1.2
// References/Option: None
#include
<
iostream
>
#include
<
iomanip
>
using
namespace
std
;
int
main
()
{
int
nmiles
;
double
emiles
;
cout
<<
"Enter the number of nautical miles: "
;
cin
>>
nmiles
;
if
(
nmiles
>
5000
)
{
cout
<<
"Enter the number of nautical miles again: "
;
cin
>>
nmiles
;
}
emiles
=
nmiles
*
1.15
;
cout
<<
fixed
<<
showpoint
;
cout
<<
nmiles
<<
" nautical miles is "
<<
setprecision
(
1
)
<<
emiles
<<
" English miles."
<<
endl
;
system
(
"pause"
);
return
0
;
}
Chegwidden/NauticalMilesTestCases.txt
Enter positive integers (-1 to quit): 89 75 65 72 78 94 63 78 83 95 81 79 86 -1 6: 3 5 7: 2 5 8 8 9 8: 1 3 6 9 9: 4 5 Plot another? (Y/N): y Enter positive integers (-1 to quit): 123 7 66 23 25 65 67 100 12345 555 -1 0: 7 2: 3 5 6: 5 6 7 10: 0 12: 3 55: 5 1234: 5 Plot another? (Y/N):
Chegwidden/Quadrants.cpp
Chegwidden/Quadrants.cpp
// Author: James Chegwidden
// Course: COSC 2436
// Program Set 1
// References: None
#include
<
iostream
>
using
namespace
std
;
int
main
()
{
int
angle
;
cout
<<
"Please enter an angle: "
;
cin
>>
angle
;
if
(
angle
>
0
&&
angle
<
90
)
cout
<<
"The angle is in quadrant I"
<<
endl
;
else
if
(
angle
>
90
&&
angle
<
180
)
cout
<<
"The angle is in quadrant II"
<<
endl
;
else
if
(
angle
>
180
&&
angle
<
270
)
cout
<<
"The angle is in quadrant III"
<<
endl
;
else
if
(
angle
>
270
&&
angle
<
360
)
cout
<<
"The angle is in quadrant IV"
<<
endl
;
else
if
(
angle
==
0
||
angle
==
90
||
angle
==
180
||
angle
==
270
)
{
switch
(
angle
)
{
case
0
:
cout
<<
"The angle is on the positive X axis"
<<
endl
;
break
;
case
90
:
cout
<<
"The angle is on the positive Y axis"
<<
endl
;
break
;
case
180
:
cout
<<
"The angle is on the negative X axis"
<<
endl
;
break
;
case
270
:
cout
<<
"The angle is on the negative Y axis"
<<
endl
;
break
;
}
}
return
0
;
}
Chegwidden/QuadrantsTestCases.txt
Enter positive integers (-1 to quit): 89 75 65 72 78 94 63 78 83 95 81 79 86 -1 6: 3 5 7: 2 5 8 8 9 8: 1 3 6 9 9: 4 5 Plot another? (Y/N): y Enter positive integers (-1 to quit): 123 7 66 23 25 65 67 100 12345 555 -1 0: 7 2: 3 5 6: 5 6 7 10: 0 12: 3 55: 5 1234: 5 Plot another? (Y/N):
Chegwidden/Sudoku.cpp
Chegwidden/Sudoku.cpp
// Author: James Chegwidden
// Course: COSC 2436
// Program Set 1
// References: None
#include
<
iostream
>
using
namespace
std
;
void
readAPuzzle
(
int
grid
[][
9
]);
bool
search
(
int
grid
[][
9
]);
int
getFreeCellList
(
const
int
grid
[][
9
],
int
freeCellList
[][
2
]);
void
printGrid
(
const
int
grid
[][
9
]);
bool
isValid
(
int
i
,
int
j
,
const
int
grid
[][
9
]);
bool
isValid
(
const
int
grid
[][
9
]);
int
main
()
{
// Read a Sudoku puzzle
int
grid
[
9
][
9
];
readAPuzzle
(
grid
);
if
(
!
isValid
(
grid
))
cout
<<
"Invalid input"
<<
endl
;
else
if
(
search
(
grid
))
{
cout
<<
"The solution is found:"
<<
endl
;
printGrid
(
grid
);
}
else
cout
<<
"No solution"
<<
endl
;
return
0
;
}
/** Read a Sudoku puzzle from the keyboard */
void
readAPuzzle
(
int
grid
[][
9
])
{
// Create a Scanner
cout
<<
"Enter a Sudoku puzzle:"
<<
endl
;
for
(
int
i
=
0
;
i
<
9
;
i
++
)
for
(
int
j
=
0
;
j
<
9
;
j
++
)
cin
>>
grid
[
i
][
j
];
}
/** Obtain a list of free cells from the puzzle */
int
getFreeCellList
(
const
int
grid
[][
9
],
int
freeCellList
[][
2
])
{
// 81 is the maximum number of free cells
int
numberOfFreeCells
=
0
;
for
(
int
i
=
0
;
i
<
9
;
i
++
)
for
(
int
j
=
0
;
j
<
9
;
j
++
)
if
(
grid
[
i
][
j
]
==
0
)
{
freeCellList
[
numberOfFreeCells
][
0
]
=
i
;
freeCellList
[
numberOfFreeCells
][
1
]
=
j
;
numberOfFreeCells
++
;
}
return
numberOfFreeCells
;
}
/** Display the values in the grid */
void
printGrid
(
const
int
grid
[][
9
])
{
for
(
int
i
=
0
;
i
<
9
;
i
++
)
{
for
(
int
j
=
0
;
j
<
9
;
j
++
)
cout
<<
grid
[
i
][
j
]
<<
" "
;
cout
<<
endl
;
}
}
/** Search for a solution */
bool
search
(
int
grid
[][
9
])
{
int
freeCellList
[
81
][
2
];
// Declare freeCellList
int
numberOfFreeCells
=
getFreeCellList
(
grid
,
freeCellList
);
if
(
numberOfFreeCells
==
0
)
return
true
;
// No free cells
int
k
=
0
;
// Start from the first free cell
while
(
true
)
{
int
i
=
freeCellList
[
k
][
0
];
int
j
=
freeCellList
[
k
][
1
];
if
(
grid
[
i
][
j
]
==
0
)
grid
[
i
][
j
]
=
1
;
// Fill the free cell with number 1
if
(
isValid
(
i
,
j
,
grid
))
{
if
(
k
+
1
==
numberOfFreeCells
)
{
// No more free cells
return
true
;
// A solution is found
}
else
{
// Move to the next free cell
k
++
;
}
}
else
if
(
grid
[
i
][
j
]
<
9
)
{
// Fill the free cell with the next possible value
grid
[
i
][
j
]
=
grid
[
i
][
j
]
+
1
;
}
else
{
// grid[i][j] is 9, backtrack
while
(
grid
[
i
][
j
]
==
9
)
{
if
(
k
==
0
)
{
return
false
;
// No possible value
}
grid
[
i
][
j
]
=
0
;
// Reset to free cell
k
--
;
// Backtrack to the preceding free cell
i
=
freeCellList
[
k
][
0
];
j
=
freeCellList
[
k
][
1
];
}
// Fill the free cell with the next possible value,
// search continues from this free cell at k
grid
[
i
][
j
]
=
grid
[
i
][
j
]
+
1
;
}
}
return
true
;
// A solution is found
}
/** Check whether grid[i][j] is valid in the grid */
bool
isValid
(
int
i
,
int
j
,
const
int
grid
[][
9
])
{
// Check whether grid[i][j] is valid at the i's row
for
(
int
column
=
0
;
column
<
9
;
column
++
)
if
(
column
!=
j
&&
grid
[
i
][
column
]
==
grid
[
i
][
j
])
return
false
;
// Check whether grid[i][j] is valid at the j's column
for
(
int
row
=
0
;
row
<
9
;
row
++
)
if
(
row
!=
i
&&
grid
[
row
][
j
]
==
grid
[
i
][
j
])
return
false
;
// Check whether grid[i][j] is valid in the 3 by 3 box
for
(
int
row
=
(
i
/
3
)
*
3
;
row
<
(
i
/
3
)
*
3
+
3
;
row
++
)
for
(
int
col
=
(
j
/
3
)
*
3
;
col
<
(
j
/
3
)
*
3
+
3
;
col
++
)
if
(
row
!=
i
&&
col
!=
j
&&
grid
[
row
][
col
]
==
grid
[
i
][
j
])
return
false
;
return
true
;
// The current value at grid[i][j] is valid
}
/** Check whether the fixed cells are valid in the grid */
bool
isValid
(
const
int
grid
[][
9
])
{
for
(
int
i
=
0
;
i
<
9
;
i
++
)
for
(
int
j
=
0
;
j
<
9
;
j
++
)
if
(
grid
[
i
][
j
]
<
0
||
grid
[
i
][
j
]
>
9
||
(
grid
[
i
][
j
]
!=
0
&&
!
isValid
(
i
,
j
,
grid
)))
return
false
;
return
true
;
// The fixed cells are valid
}
Chegwidden/SudokuTestCases.txt
Enter positive integers (-1 to quit): 89 75 65 72 78 94 63 78 83 95 81 79 86 -1 6: 3 5 7: 2 5 8 8 9 8: 1 3 6 9 9: 4 5 Plot another? (Y/N): y Enter positive integers (-1 to quit): 123 7 66 23 25 65 67 100 12345 555 -1 0: 7 2: 3 5 6: 5 6 7 10: 0 12: 3 55: 5 1234: 5 Plot another? (Y/N):