-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNebApp.py
More file actions
237 lines (208 loc) · 7.53 KB
/
NebApp.py
File metadata and controls
237 lines (208 loc) · 7.53 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import imp
from multiprocessing import managers
import os
from threading import main_thread
from unicodedata import name
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
import os
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.clock import Clock
from aiclass import gamestructure
player = ''
class Gamescreen(Screen):
idd = 0
global player
def on_enter(self, *args):
self.gamedata = Menuscreen().retdata()
print(player)
self.ids.log_id.text = "The Game has Started"
if player == "Human":
self.playeract()
elif player == "Computer":
self.comp_act()
def playeract(self,val = 0):
if not self.gamedata.chkwin():
if val == 0:
self.btnadder()
self.ids.comp_id.text = str(self.gamedata.score[0])
self.ids.hum_id.text = str(self.gamedata.score[1])
else:
retval = self.gamedata.gamestate(2,val)
self.ids.bx_lyt.clear_widgets()
if retval == 200:
self.comp_act()
elif retval == 100:
self.ids.log_id.text = "Computer Wins the game"
self.ids.comp_id.text = str(self.gamedata.score[0])
self.ids.hum_id.text = str(self.gamedata.score[1])
self.gamedata.restetparm()
elif retval == -100:
self.ids.log_id.text = "Human Wins the game"
self.ids.comp_id.text = str(self.gamedata.score[0])
self.ids.hum_id.text = str(self.gamedata.score[1])
self.gamedata.restetparm()
elif retval == 50:
self.ids.log_id.text = "The game is a draw"
self.ids.comp_id.text = str(self.gamedata.score[0])
self.ids.hum_id.text = str(self.gamedata.score[1])
self.gamedata.restetparm()
else:
print("Game is finished")
self.gamedata.restetparm()
def comp_act(self):
if not self.gamedata.chkwin():
bestscore = -10
bestnum = 0
for num in self.gamedata.numstring:
self.gamedata.numstring.remove(num)
self.gamedata.score[0] = self.gamedata.score[0] - num
score = self.gamedata.minimax(False)
self.gamedata.numstring.append(num)
self.gamedata.score[0] = self.gamedata.score[0] + num
if(score > bestscore):
bestscore = score
bestnum = num
self.ids.log_id.text = "Computer Choose " + str(bestnum)
retval = self.gamedata.gamestate(1,bestnum)
if retval == 200:
self.playeract()
elif retval == 100:
self.ids.log_id.text = "Computer Wins the game"
self.ids.comp_id.text = str(self.gamedata.score[0])
self.ids.hum_id.text = str(self.gamedata.score[1])
self.gamedata.restetparm()
elif retval == -100:
self.ids.log_id.text = "Human Wins the game"
self.ids.comp_id.text = str(self.gamedata.score[0])
self.ids.hum_id.text = str(self.gamedata.score[1])
self.gamedata.restetparm()
elif retval == 50:
self.ids.log_id.text = "The game is a draw"
self.ids.comp_id.text = str(self.gamedata.score[0])
self.ids.hum_id.text = str(self.gamedata.score[1])
self.gamedata.restetparm()
else:
print("Game is finished")
self.gamedata.restetparm()
def btnadder(self):
xax = 0.1
for x in self.gamedata.numstring:
xax = xax + 0.1
button = Button(text=str(x),font_size = 12,size_hint=(0.1,0.1),pos_hint={'x': xax, 'y': 0.7},on_press= self.scorbtn)
button.my_id = x
self.ids.bx_lyt.add_widget(button)
def restartfn(self):
Window.clearcolor = (0, 0, 0, 0)
self.ids.bx_lyt.clear_widgets()
self.manager.current = 'menu'
def scorbtn(self,instance):
print(instance)
self.playeract(instance.my_id)
def refresh(self):
self.idd = self.idd + 1
print(self.idd)
self.ids.some_d.text = str(self.idd)
def func(self):
return str(self.idd)
class Menuscreen(Screen):
gamedata = gamestructure()
def on_enter(self, *args):
self.gamedata = gamestructure()
def retdata(self):
return self.gamedata
def do_work(self,ply):
global player
player = ply
self.manager.transition = SlideTransition(direction ="right")
self.manager.current = 'Game'
Window.clearcolor = (0.247, 0.247, 0.247, 1)
def exitfn(self):
exit()
Builder.load_string("""
<Menuscreen>:
FloatLayout:
cols: 1
size: root.height, root.width
Label:
text: "[b][color=04ff00]Welcome to the Number Game[/color][/b]"
pos_hint: {'x': 0, 'y': 0.4}
markup: True
font_size: 48
Label:
text: "[color=f2ff00] Who wants to play first?[/color] "
markup: True
font_size: 32
pos_hint: {'x': 0, 'y': 0.3}
Button:
text: "Human"
font_size: 24
pos_hint: {'x': 0.4, 'y': 0.5}
size_hint: 0.2, 0.2
on_press: root.do_work("Human")
Button:
text: "Computer"
font_size: 24
pos_hint: {'x': 0.4, 'y': 0.3}
size_hint: 0.2, 0.2
on_press: root.do_work("Computer")
Button:
text: "Exit"
font_size: 24
size: 75,50
background_color: (1.0, 0.0, 0.0, 1.0)
size_hint: None,None
on_press: root.exitfn()
<Gamescreen>:
FloatLayout:
id: gm_scr
cols: 1
size: root.height, root.width
Label:
text: "ScoreBoard"
pos_hint: {'x': 0, 'y': 0.45}
font_size: 24
Label:
text: "Computer: "
pos_hint: {'x': -0.4, 'y': 0.35}
font_size: 20
Label:
id: comp_id
text: "0"
pos_hint: {'x': -0.3, 'y': 0.35}
font_size: 20
Label:
text: "Human: "
pos_hint: {'x': 0.3, 'y': 0.35}
font_size: 20
Label:
id: hum_id
text: "0"
pos_hint: {'x': 0.4, 'y': 0.35}
font_size: 20
BoxLayout:
id: bx_lyt
Label:
id: log_id
text: "Game Has started"
pos_hint: {'x': 0, 'y': 0.1}
font_size: 20
Button:
text: "Restart"
font_size: 24
size: 75,50
background_color: (1.0, 0.0, 0.0, 1.0)
size_hint: None,None
on_press: root.restartfn()
""")
Window.clearcolor = (0, 0, 0, 0)
class NebApp(App):
def build(self):
manager = ScreenManager()
manager.add_widget(Menuscreen(name='menu'))
manager.add_widget(Gamescreen(name="Game"))
return manager
if __name__ == '__main__':
NebApp().run()