From 71e22504ab3c199d91e8c2f37d50b48dc2b73902 Mon Sep 17 00:00:00 2001 From: Cory Date: Sat, 18 May 2024 21:59:57 -0400 Subject: [PATCH] I made everything spawn on turn 1 using the Spawner class. When you press enter, you're told if the space is clear or is a mine, in which case you lose. Next step is to reveal the numbers. --- __pycache__/cursor.cpython-312.pyc | Bin 1422 -> 1387 bytes __pycache__/free_space.cpython-312.pyc | Bin 0 -> 1102 bytes __pycache__/mine.cpython-312.pyc | Bin 993 -> 1130 bytes __pycache__/mine_spawner.cpython-312.pyc | Bin 1224 -> 2418 bytes cursor.py | 8 +++--- free_space.py | 18 +++++++++++++ mine.py | 9 ++++--- mine_spawner.py | 31 +++++++++++++++++++---- minesweeper_game.py | 8 +++--- 9 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 __pycache__/free_space.cpython-312.pyc create mode 100644 free_space.py diff --git a/__pycache__/cursor.cpython-312.pyc b/__pycache__/cursor.cpython-312.pyc index d0d3dc80e490efcecb5600cebd754f3502c18ccc..a657f0fc7258e60438dc6e44638055d1cfbcbaa3 100644 GIT binary patch delta 496 zcmeCfUQ@jsI0A0*4++W*S+sV?w*kSl!vNB7T+67sIEyf#4w%2Z~ zy=?4yQP%aUh}#EN1`+)a%nSnBAA}ird>M9tRVwKt{1RkZ57J!7be2azjA4!SfRfBdg+PHhxxjps6sGz#xZc wQv`{?jrzr5lbfGXnv-f*qy&_Z2L(g1AdvXL%*e?2olA_-o$(U`hy?2a0GeoQg#Z8m delta 539 zcmX|+L1+_E5Qg8pw_7&5-E`@O)?m@5jfZ7JMW`p!oK|VpHBm#gNb( zdPoH|raw%TZz_^lOz^e^xJXZ!-*KCs@pzI{{or}3+6WaKFgv^dtM+IPRfk0R zg}#>S8ol)>n?Hhn);TJkf0D|BvecE%BQgFed&|cX|?Ma81IN$RHe*r^I76r#NF=7q8eNq{UaZkTtOu z9wTtY3Hw8Puf3;qp?fb9L-zLO$ETk+K5hKWxC51B?hY8S+<{HTSAkpNP;qyr&={x6 z)eT<2NG9a kTCXQvXvG(G?`6$j=6A3FGl)?BIbZ=}w z^jidxDWNl%0;h*0w2o}FhirTVCxsPJThe7~7z`1I!QeJHJw#Cv+o&knxG39FQL*Jc zRE*gQ;Fui)jJL=1xSv>Kls;>k7WMJmrWKYCN(oA0X{Uv0YF z@f^3IOTNNsZJUijmJ6;xvWUNqL_1vaWH>*ygM0OmnOf1)?Mt#S+ zXS5oQci(6-w?ZwCl*~FcwxKz~MRyC$wyzT6z!^e16X#toM9&i9wE}ige@c6!yh{%t zK$e&wvKTnQM2Fz?P#0IC%*d~eUn92Cl`5mmFyBR$=+lfajza1A9bm;r5~1sfwgUS< z-2vOD{!9L6*63$)J#}CGm_6A1wDn=@$MpPR?RfgZugt>}8RhN* zq~@8}e=AyB@L|vY**N?1#z}=WSh=|E@Mg_y>o}N>iwfZ=O4+e#=gLU?5MP6Qv>B5# zuo`W*8zlrJ#7`1ZckLF)K>QRTuUcj;%p|O`$xO?mj9rIHOngk16>tOK8w!dA zN-`m90AkVjEr31L&t!Ku`d21)*H0B3n>`(qV~c|%j^&3uL4p6#kSZ7#<<8W_VN1(w X1`=XSeih(E#u)!erg8oh0R)7<60_>^ literal 0 HcmV?d00001 diff --git a/__pycache__/mine.cpython-312.pyc b/__pycache__/mine.cpython-312.pyc index 8950b8bf3e9f7fbb011b8d961a9f57b99c133a6d..e770c25890d2aeab33f2545fa9fc5bd2fd49973e 100644 GIT binary patch delta 368 zcmaFJ{)&U|G%qg~0}yp?~)GlI;80wy5M z48)&>fW&l$6vi5cc&HQuLnVVIlb@R=<1O}-%;JKa#7a%ZB6gr`F~|gkA`T#VizOp7 zCAEqZ;QxGK22_zaAZivftu=Mg2fh_gYWWU7`KiPm;sh%@ABe5tkxg@oy2&Aq^ z6hue>iIogsrC@W5K#l?vtUyMw7?5aTxFI0i;Q5J#kyY^%8w;!SXC6jYS%?;pYCoXs rz`A5XT0xeAP5Q-QlbfGXnv-f*BnK2@oP3m-pO=?W_7ek;04oCk?odl! delta 239 zcmaFG@sOSGG%qg~0}vby@JzeLIFZkXQD$PaEO#Y~ChIL0-^{#Jw~5^@jI0|kEMgRA z1**HnoRgn^i@CU@=oWKoUWz7j5!>WWCRs-I$;+AA**SqK{507nuVPlMXHUs2F33r& zEaCtP7Ks3fl?+871x4&2f*D4DOf41!5-kij_=FqWKd~^fDt=~TVU>oc1}XH@WQ1st u1nGhs@{7YJH$SB`C)KV<3Mjz{#KrtT;sY}yqX6S~E-pstPYgf;tOEd18ZyZM diff --git a/__pycache__/mine_spawner.cpython-312.pyc b/__pycache__/mine_spawner.cpython-312.pyc index 12e8251537d4a416f7a982d28501df16fa4360b8..39cb7b276f153298cbae26932a215d7858762e2e 100644 GIT binary patch literal 2418 zcmb7GO=ufe5Pna)E6bMT$gLd7QJmOu>~`bSN~-+S!A&aS#;tMG<`*hzwrlNLUR!_J zT_woKkV6g$7;4jl6$sQl)g>20Dd?C&X#<7gWo=}Z1On|PH`h)nHQ-viH^ zo%hYWnRzo~{p|HNA+o%qACCXxMCe!euoqorwmu-t0uqpbQz(t&7z6J}Inq>|GI=UR zr^u-Isj`HUcCRKn61rHn}S#tX72 zUdizZ_;8-j%Su*GSh^O))sLtjNck`pRR8?+*(#*HRRsz}qK!B{}21yLZQ9b~)j#%v?EcCL|`Urna~AP z#FGHowFEr%CPH&q{!s<2xr%7gt{huL_J`F}4x-UBknkONQhB@AHhc%cQ!4mlE_kSM zhkZUn6VKfCS#2-UHUgvBLo}Klpi%8k;gvq|=UdYsudS=@u{nXNAB%veN!Qp_y$fF_ zmYw)*zB9!47W=l^USuX;t9{vAEgquPYD;VTKWiyrq7|s2ZH`FWY`(TXtg`iOf`UK+ z-QiQcS72(0+3&W$K>f_xX!0t;|E!W>vr0mC4F3|BSwq_+5U8osq*3r}_kY#2GB63P zwQJegBb7HoDsP;I?x~X<^`Na-KO0uvkh8z7sah@BnZsmMkzqy0oiL$|v>6;W~^Bi208A6OkUJy7wDP~j|dRT_0HmFoKX}EG(MN*|~#&Agr zC#G|1!SKjpI(t*J6pXz|QMFiw8!K?H@Kj3781#fBh%&^%I%N-7rD2d6STgXW(KJrx zKFqU(XwWd*6hOkH?C6kvfNspC_yPw3Ot;t*?v)6dMOh;8-bT;ddn)db<_?u^=)8r{%+0Ry(m?>YZy@TZI4U(|XouUxsR^;}!^Uk5AA^UWo7 z&3AaiOPqP(1J`zjI)v-RN~F;z0W&S9w9qL%^iH|694)tMp>ulRy+=_!@cw#me*?OdvI#;M5QaDbw!qW zt?P8zU+Ev#`iCE1()wd7hd$CnAFp{XZLrAGDZ`7!SSS47%8)YPIHQT<(pe#&0^Y-M zH}ZU{deR_B3Ouz8lTz*`ywgp!Me0Zx3zK4rXA>wU@b!EjWN; zY@^|XA0FAXS={nrBD89~3ple2oC))nhgq(c{UdM&w#_?Vx&|y?&&qER88n&#t-L{D cjQ{W;9D0gcpQ4VZsO?X>9bd+q2&ie`FXVsp82|tP delta 668 zcmew)bb^!bG%qg~0}#yc^Gq{gn#d=?XfRP-)_{q@ogsy}g&~DyH4{jhfgy^el2w!K zC5WlXc#FL#F)t-EuLQ_s@y*Ojow(SNl^e{s`&CQ?#0V}(EYC|V(u1_Vgy~ol*Kdo0<%;oBLh$YKSBb=s$p8q2w^iaR05$Uv)@aQKoQ7~njE*dOG=CK z;`2&#lTwR{SU?gW#}$DbUBo$AkxjUsGchG4J~2HN=yT>GP{7?{Nl(m8y~UCX^l~vs zwSs~|5g$l`vmht2G9F}Nl_+w6z=Z{Xs)|#AL<7SOZvN}s(igd3B5y%B##}=`H zSRjHEh>Hb*L<_?WKH&!UPb^HVHlI0ISb_RsYWy@=Z?S A2mk;8 diff --git a/cursor.py b/cursor.py index c4b5765..3d152b7 100644 --- a/cursor.py +++ b/cursor.py @@ -6,12 +6,12 @@ class Cursor: name = "cursor" character = 'O' - def __init__(self, board_size): - board_width, board_height = board_size - self.position = (board_width // 2, board_height - 1) + def __init__(self, position): + self.position = position def handle_keystroke(self, keystroke, game): x, y = self.position + if keystroke.name in ("KEY_LEFT", "KEY_RIGHT", "KEY_UP", "KEY_DOWN"): if keystroke.name == "KEY_LEFT": new_position = (x - 1, y) @@ -24,4 +24,4 @@ class Cursor: if game.on_board(new_position): if game.is_empty(new_position): self.position = new_position - game.log(self.position) \ No newline at end of file + game.log("The cursor is at " + str(self.position)) \ No newline at end of file diff --git a/free_space.py b/free_space.py new file mode 100644 index 0000000..db53f85 --- /dev/null +++ b/free_space.py @@ -0,0 +1,18 @@ +# free_space.py +# ------------ +# By Cory +# This module defines a free space agent class. It displays if enter is pressed while the cursor is on it. +class FreeSpace: + character = '*' + display = True + + def __init__(self, position): + self.position = position + + def handle_keystroke(self, keystroke, game): + if keystroke.name in ("KEY_ENTER"): + if not game.is_empty(self.position): + game.log(str(self.position) + " is clear!") + + def hide(self): + self.display = False \ No newline at end of file diff --git a/mine.py b/mine.py index 679d829..b8eefc9 100644 --- a/mine.py +++ b/mine.py @@ -1,9 +1,9 @@ # mine.py # ------------ # By Cory -# This module defines a mine agent class. It doesn't do anything except exist in a position +# This module defines a mine agent class. It ends the game if enter is pressed while the cursor is over it. class Mine: - display = False + character = '_' def __init__(self, position): self.position = position @@ -12,4 +12,7 @@ class Mine: if keystroke.name in ("KEY_ENTER"): if not game.is_empty(self.position): game.log("You hit a mine at " + str(self.position) + ".") - game.end() \ No newline at end of file + game.end() + + def hide(self): + self.display = False \ No newline at end of file diff --git a/mine_spawner.py b/mine_spawner.py index ecdceb3..3c25a27 100644 --- a/mine_spawner.py +++ b/mine_spawner.py @@ -1,12 +1,14 @@ # mine_spawner.py # ------------ # By Cory -# This module defines a mine spawner agent class. It spawns mines! +# This module defines a spawner agent class. It spawns mines and then fills the remaining spots with free spaces from random import randint from mine import Mine +from free_space import FreeSpace +from cursor import Cursor -class MineSpawner: +class Spawner: display = False def __init__(self, board_size): @@ -14,7 +16,26 @@ class MineSpawner: self.board_width, self.board_height = width, height def play_turn(self, game): + # On the first turn we will spawn everything. if game.turn_number == 1: - mine = Mine(((randint(0, self.board_width - 1)),(randint(0, self.board_height - 1)))) - game.log(mine.position) - game.add_agent(mine) \ No newline at end of file + # First spawn free spaces everywhere + for i in range(self.board_width): + for j in range(self.board_height): + free_space = FreeSpace((i,j)) + game.add_agent(free_space) + # Then spawn 10 mines + for i in range(10): + mine = Mine(((randint(0, self.board_width - 1)),(randint(0, self.board_height - 1)))) + game.log(mine.position) + # If there is a free space where a mine is going to be, remove the free space and then add the mine. + if not game.is_empty(mine.position): + game.remove_agent(game.get_agents_by_position()[mine.position][0]) + game.add_agent(mine) + # Now ask all of the mines and free spaces to hide themselves. + for i in range(self.board_width): + for j in range(self.board_height): + if len(game.get_agents_by_position()[(i,j)]) != 0: + game.get_agents_by_position()[(i,j)][0].hide() + # Now create a cursor. + game.add_agent(Cursor((self.board_width - 1, self.board_height - 1))) + diff --git a/minesweeper_game.py b/minesweeper_game.py index b6f99b4..92f3894 100644 --- a/minesweeper_game.py +++ b/minesweeper_game.py @@ -3,12 +3,10 @@ # By Cory # This class implements a simple minesweeper game on a 9x9 gird. from retro.game import Game -from cursor import Cursor -from mine_spawner import MineSpawner +from mine_spawner import Spawner board_size = (9, 9) -cursor = Cursor(board_size) -spawner = MineSpawner(board_size) +spawner = Spawner(board_size) # spawner = AsteroidSpawner(board_size) -game = Game([cursor,spawner], {"score": 0}, board_size=board_size,debug=True) +game = Game([spawner], {"score": 0}, board_size=board_size,debug=True) game.play() \ No newline at end of file