-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
160 lines (128 loc) · 5.21 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require "./game.rb"
require "./card.rb"
require "./deck.rb"
require "./player.rb"
# deck = Deck.new
# puts deck.length
# puts deck.shuffle
class Main
puts "Welcome to the game of Set"
puts ""
puts "Would you like to start a new game? (1 - yes, 2 - no)"
gameChoice = gets.chomp.to_i
if gameChoice == 1
game = Game.new
playerCount = game.getPlayers
playerDeck = Deck.new
i = 1
#Creates a player object for each player and adds to an array holding all players
while i <= playerCount do
puts "Enter player #{i}'s name: "
name = gets.chomp
playerDeck.push(Player.new("Player #{i}: "+ name, 0, "player"))
i += 1
end
botCount = game.getBots(playerCount)
playerCount += botCount
i = 1
#Creates a bot object for each bot and adds to an array holding all players
while i <= botCount do
puts "Enter bot #{i}'s name: "
name = gets.chomp
playerDeck.push(Player.new("Bot #{i}: "+ name, 0, "bot"))
i += 1
end
#Creates the original deck with 81 cards and randomizes the order of cards
cardDeck = Deck.new
cardDeck.createFullDeck
cardDeck.shuffle
tableDeck = Deck.new
gameStatus = true
totalPoints = 0
playerTurn = 1
#Starts the game with a total of 9 cards on the table
for i in 0..3 do
game.dealCards(cardDeck, tableDeck)
end
#while loop that will be going forever until game ends
while gameStatus
if playerTurn > playerCount
playerTurn = 1
end
sleep 0.5
puts ""
puts "It is #{playerDeck.index(playerTurn - 1).name}'s turn!"
puts "Current score: #{playerDeck.index(playerTurn - 1).score}"
puts ""
#Displays cards on the table for the player
for i in 1..tableDeck.length do
puts "#{i}: " + tableDeck.index(i - 1).to_s
end
staleMate = false
#check the table for sets when there are no more cards left to be drawn
if cardDeck.length == 0 && game.verifyTable(tableDeck) == false
sleep 1
puts ""
puts "There are no more possible sets."
sleep 0.5
staleMate = true
else
# Checks first if player is human or bot.
# Asks the current player what their choice is,
# Adds a point to the player and totalpoints
# if the method returns true
if (playerDeck.index(playerTurn - 1).type == "player" && playerDeck.index(playerTurn - 1).getChoice(tableDeck, cardDeck, game) == true) || (playerDeck.index(playerTurn - 1).type == "bot" && playerDeck.index(playerTurn - 1).botChoice(tableDeck, playerDeck.index(playerTurn - 1).name, cardDeck, game) == true)
playerDeck.index(playerTurn - 1).addScore(1)
totalPoints += 1
game.dealCards(cardDeck, tableDeck) #Deal 3 new cards to the table
end
end
playerTurn += 1
if totalPoints == 27 || staleMate == true
puts "Game over"
winner = Player.new(playerDeck.index(0).name, playerDeck.index(0).score, playerDeck.index(0).type)
tieCount = 0
for i in 1..(playerDeck.length - 1) do
if playerDeck.index(i).score > winner.score
winner.setScore(playerDeck.index(i).score)
winner.setName(playerDeck.index(i).name)
elsif playerDeck.index(i).score == winner.score
tieCount += 1
end
end
puts ""
if tieCount != playerDeck.length - 1 || playerDeck.length == 1
puts "The winnner is #{winner.name} with a score of #{winner.score}!"
else
puts "The game is a tie..."
end
puts ""
puts "Would you like to play again? (1 - yes, 2 - no)"
gameChoice = gets.chomp.to_i
if gameChoice == 1
gameStatus = true;
#everything below is to reset the game
playerTurn = 1
totalPoints = 0
cardDeck = Deck.new
cardDeck.createFullDeck
cardDeck.shuffle
tableDeck = Deck.new
#Starts the game with a total of 9 cards on the table
for i in 0..3 do
game.dealCards(cardDeck, tableDeck)
end
#Reset individual player scores
for i in 0..(playerDeck.length - 1) do
playerDeck.index(i).setScore(0)
end
else
gameStatus = false;
end
end
end
puts "Game Ending..."
else #game will end
puts "Game Ending..."
end
end