-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.java
158 lines (138 loc) · 4.04 KB
/
game.java
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
/*
* Copyright 2009 James Koval
*
* This file is part of Jedi Mage
*
* Jedi Mage is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Jedi Mage is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jedi Mage. If not, see <http://www.gnu.org/license/>
*/
import java.util.ArrayList;
public class game {
public ArrayList<player> players;
public int turn;//Holds index of currentplayer
public player currentplayer;
public boolean tie, gameover;
public game(){
init();
}
void init(){
turn = -1;
players = new ArrayList<player>();
}
public void add(player wizard){
players.add(new player(wizard));
}
public void start(){
//Ready players for the first turn where their upkeep brings their stats back to starting values
for(int i = 0; i < players.size(); i ++){
players.get(i).beast -= players.get(i).zoo;
players.get(i).gem -= players.get(i).magic;
players.get(i).brick -= players.get(i).quarry;
for(int j = 0; j < 6; j++)
players.get(i).draw();
}
}
public boolean quickstart(int numberofplayers){
deckgenerator dg = new deckgenerator();
if(dg.decks == null || dg.decks.length == 0)//if no decks are found return false
return false;
deck library = null;
for (int i = 0; i < dg.decks.length; i++){//Get quickstart deck to use
if(dg.decks[i].equalsIgnoreCase("quickstart.txt"))
if(dg.open(dg.decks[i]))
library = dg.library;
}
if(library == null)//if quickstart deck isn't found return false
return false;
for(int i = 0; i < numberofplayers; i++){//Quickly add two players with standard decks
player wizard = new player("Player "+i);
wizard.usedeck(library);
this.add(wizard);
}
return true;
}
public void newturn(){
turn ++;
if(turn == players.size())
turn = 0;
currentplayer = players.get(turn);
currentplayer.upkeep();
}
public boolean canplay(card c){
return players.get(turn).canplay(c);
}
/**
* Assumed: Can play the card
* @param cardindex
* @param targetindex
* @return false:can't pay cost
*/
public void playcard(int cardindex, int targetindex){
//Determine targets: you, enemy
player you = currentplayer;
player enemy = players.get(targetindex);
//Remove the card from the player's hand
card ctemp = you.hand.play(cardindex);
//See if the card is one with effects
cardwitheffects c;
try{c = (cardwitheffects)ctemp;}
catch(Exception e){return;}
you.pay(c.cost);
for(int i = 0; i < c.effects.size(); i++){
switch(c.effects.get(i).who){
case you: you.add(c.effects.get(i).vs); break;
case enemy: enemy.add(c.effects.get(i).vs); break;
case all: for(int j = 0; j < players.size(); j++)
players.get(j).add(c.effects.get(i).vs);
break;
}
}
you.checkhandsize();
}
public void playcard(int cardindex){
//Determine targets: you
player you = currentplayer;
System.out.println("You are "+you.name);
//Remove the card from the player's hand
cardwitheffects c = (cardwitheffects)you.hand.play(cardindex);
you.pay(c.cost);
for(int i = 0; i < c.effects.size(); i++){
switch(c.effects.get(i).who){
case you: you.add(c.effects.get(i).vs);break;
case all: for(int j = 0; j < players.size(); j++)
players.get(j).add(c.effects.get(i).vs);
break;
}
}
you.checkhandsize();
}
public void remove(int index){
players.remove(index);
turn--;
}
/**
* @return winning player or null if none
*/
public player winner(){
int living = 0;
player victor = null;
for(int i = 0; i < players.size(); i++)
if(players.get(i).alive){
living++;
victor = players.get(i);
}
if(living == 1)
return victor;
return null;
}
}