-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameLogic.v
208 lines (146 loc) · 5.59 KB
/
gameLogic.v
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
`timescale 1ps/1ps
//changing 3 bit to 6 bit
module gameLogic(
playerLED1,
playerLED2,
player1,
player2,
clock,
reset,
player1_position,
player2_position,
player1_health,
player2_health,
winner1,
winner2
);
output reg[2:0] playerLED1; //Player 1 (samte chap) 2 1 0 == _ _ _
output reg[0:2] playerLED2; // Player 2 (samte rast) 0 1 2 == _ _ _
//3 bit
//input[2:0] player1; //action
//input[2:0] player2; //action
//6 bit
input[5:0] player1; //action
input[5:0] player2; //action
input clock;
input reset;
output reg[2:0] player1_position; //position starts from 0
output reg[2:0] player2_position; //position starts from 5
output reg[1:0] player1_health, player2_health; //health starts from 3
output reg winner1;//winner 1
output reg winner2; //winner 2
//current state of players and their last actions
reg [5:0] current_player1 , current_player2;
reg[5:0] last_player1, last_player2;
///@encoding for the 3bit inputs for each player
//3 bit
//parameter Wait = 3'b000 , Punch = 3'b001 , Kick = 3'b010 , Move_Left = 3'b011 , Move_Right = 3'b100 , Jump = 3'b101;
//6 bit
parameter Wait =6'b100000 , Move_Left = 6'b010000 , Move_Right = 6'b001000 , Jump = 6'b000100 , Punch = 6'b000010 , Kick = 6'b000001;
initial begin
/*
* Position (array)
* 0 1 2 | 3 4 5
*
* Health = 3
*
*/
player1_position= 0;
player2_position = 5;
player1_health = 3;
player2_health = 3;
//initial state
current_player1 = Wait;
current_player2 = Wait;
//initial last state
last_player1 = Wait;
last_player2 = Wait;
//initial winner state
winner1 = 0;
winner2 = 0;
end
always @(posedge clock) begin
//revert back to the first state of game
if (reset) begin
player1_health = 3;
player2_health = 3;
player1_position = 0;
player2_position = 5;
current_player1 = Wait;
current_player2 = Wait;
last_player1 = Wait;
last_player2 = Wait;
end
else begin
//if not reset , apply the actions
current_player1 = player1;
current_player2 = player2;
end
//player 1 movement
if (current_player1 == Move_Left && player1_position > 0)
player1_position = player1_position - 1; //He can move to the left
else if (current_player1 == Move_Right && player1_position < 2 )
player1_position = player1_position + 1;
//player 2 movement
if(current_player2 == Move_Left && player2_position > 3)
player2_position = player2_position - 1;//He can move to the left
else if(current_player2 == Move_Right && player2_position < 5)
player2_position = player2_position + 1;
//attacks in 1 range _ _ *|* _ _
if (player2_position - player1_position == 1 && current_player1 != Move_Left && current_player2 != Move_Right) begin
//Punch (olaviat)
if(current_player1 == Punch && current_player2 != Jump && current_player2 != Punch) player2_health = player2_health - 2;
else if(current_player2 == Punch && current_player1 != Jump && current_player1 != Punch) player1_health = player1_health - 2;
//Kick
else if(current_player1 == Kick && current_player2 != Jump && current_player2 != Kick && current_player2 != Punch) player2_health = player2_health -1;
else if(current_player2 == Kick && current_player1 != Jump && current_player1 != Kick && current_player1 != Punch) player1_health = player1_health -1;
//if both Punch ==> knockback
else if(current_player1 == Punch && current_player2 == Punch)begin
player1_position = player1_position - 1 ;
player2_position = player2_position + 1;
end
//if both Kick ==> Knockback
else if(current_player1 == Kick && current_player2 == Kick)begin
player1_position = player1_position - 1 ;
player2_position = player2_position + 1;
end
end
//attacks in 2 range _ * _|* _ _
if (player2_position - player1_position == 2 && current_player1 != Move_Left && current_player2 != Move_Right) begin
if (current_player1 == Kick && current_player2 != Kick && current_player2 != Jump) player2_health = player2_health - 1;
else if(current_player2 == Kick && current_player1 != Kick && current_player1 != Jump) player1_health = player1_health - 1;
//if both Kick ==> Knockback
else if(current_player1 == Kick && current_player2 == Kick) begin
player1_position = player1_position - 1;
player2_position = player2_position + 1;
end
end
//Wait 1
if(current_player1 == Wait && last_player1 == Wait)begin
if(player1_health < 3) player1_health = player1_health + 1;
end
last_player1 = current_player1;
//Wait 2
if(current_player2 == Wait && last_player2 == Wait)begin
if(player2_health < 3)player2_health = player2_health + 1;
end
last_player2 = current_player2;
end
//Checking Winner
always @(posedge clock) begin
//if player 1 dies , player 2 wins
if(player1_health == 0 && player2_health != 0) winner2 = winner2 + 1;
//if player 2 dies , player 1 wins
else if(player2_health == 0 && player1_health != 0) winner1 = winner1 + 1;
end
always@(player1_position) begin
if(player1_position == 0)playerLED1 = 3'b100;
else if(player1_position == 1)playerLED1 = 3'b010;
else if(player1_position == 2)playerLED1 = 3'b001;
end
always@(player2_position) begin
if(player2_position == 5)playerLED2 = 3'b001;
else if(player2_position == 4)playerLED2 = 3'b010;
else if(player2_position == 3)playerLED2 = 3'b100;
end
endmodule