aboutsummaryrefslogtreecommitdiffstats
path: root/sandbox
diff options
context:
space:
mode:
authorMatt Strapp <strap012@umn.edu>2021-04-26 10:53:43 -0500
committerMatt Strapp <strap012@umn.edu>2021-04-26 15:03:12 -0500
commitd311af01feb32550aaae8638d4cc167948f5464c (patch)
tree3c0b8606a7a5267e3e890a63b8565c5c27f10438 /sandbox
parentactually add files (diff)
downloadcsci4511w-d311af01feb32550aaae8638d4cc167948f5464c.tar
csci4511w-d311af01feb32550aaae8638d4cc167948f5464c.tar.gz
csci4511w-d311af01feb32550aaae8638d4cc167948f5464c.tar.bz2
csci4511w-d311af01feb32550aaae8638d4cc167948f5464c.tar.lz
csci4511w-d311af01feb32550aaae8638d4cc167948f5464c.tar.xz
csci4511w-d311af01feb32550aaae8638d4cc167948f5464c.tar.zst
csci4511w-d311af01feb32550aaae8638d4cc167948f5464c.zip
Rebase newer branch
Diffstat (limited to '')
-rw-r--r--sandbox/game.py282
-rw-r--r--sandbox/pics/A.pngbin409 -> 0 bytes
-rw-r--r--sandbox/pics/B.pngbin359 -> 0 bytes
-rw-r--r--sandbox/pics/X.pngbin453 -> 0 bytes
-rw-r--r--sandbox/pics/block.pngbin109 -> 0 bytes
-rw-r--r--sandbox/pics/empty.pngbin145 -> 0 bytes
-rw-r--r--sandbox/pics/lineX.pngbin110 -> 0 bytes
-rw-r--r--sandbox/pics/lineXempty.pngbin133 -> 0 bytes
-rw-r--r--sandbox/pics/lineY.pngbin110 -> 0 bytes
-rw-r--r--sandbox/pics/lineYempty.pngbin127 -> 0 bytes
10 files changed, 0 insertions, 282 deletions
diff --git a/sandbox/game.py b/sandbox/game.py
deleted file mode 100644
index 003d943..0000000
--- a/sandbox/game.py
+++ /dev/null
@@ -1,282 +0,0 @@
-import pygame
-import numpy as np
-import sys
-
-
-class Game:
- def __init__(self):
- self.grid_size = 10 # default
- if len(sys.argv) > 1:
- self.grid_size = int(sys.argv[1])
-
- # It turns out that there are nice structures when setting ~0.75 walls per slot
- self.start_walls = int(0.75 * self.grid_size ** 2)
-
- self.accept_clicks = True
-
- # variables for the boxes for each player (x would be computer)
- self.a_boxes = 0
- self.b_boxes = 0
- self.x_boxes = 0
-
- self.turn = "X"
- self.caption = "'s turn "
-
- # 0 empty 1 is A 2 is B and 3 is X
- self.grid_status = np.zeros((self.grid_size, self.grid_size), np.int)
- self.upper_walls_set_flags = np.zeros((self.grid_size, self.grid_size), np.dtype(bool))
- self.left_walls_set_flags = np.zeros((self.grid_size, self.grid_size), np.dtype(bool))
-
- # set the outer walls
- for column in range(self.grid_size):
- for row in range(self.grid_size):
- if column == 0:
- self.left_walls_set_flags[column][row] = True
- if row == 0:
- self.upper_walls_set_flags[column][row] = True
-
- # initialize pygame
- pygame.init()
-
- # set the display size (one slot has 30x30 pixels; Walls: 4x26 Box: 26x26)
- self.screen = pygame.display.set_mode([30 * self.grid_size + 4, 30 * self.grid_size + 4])
-
- # load all images
- self.empty = pygame.image.load("pics/empty.png")
- self.A = pygame.image.load("pics/A.png")
- self.B = pygame.image.load("pics/B.png")
- self.X = pygame.image.load("pics/X.png")
- self.block = pygame.image.load("pics/block.png")
- self.lineX = pygame.image.load("pics/lineX.png")
- self.lineXempty = pygame.image.load("pics/lineXempty.png")
- self.lineY = pygame.image.load("pics/lineY.png")
- self.lineYempty = pygame.image.load("pics/lineYempty.png")
-
- tries = 0
- # set the start walls randomly but do not create any opportunity to directly close boxes
- while self.start_walls > 0 and tries < 4*self.grid_size**2:
- x = np.random.randint(self.grid_size)
- y = np.random.randint(self.grid_size)
- up = np.random.randint(2)
-
- if up:
- if not self.upper_walls_set_flags[x][y] \
- and self.get_number_of_walls(x, y) < 2 \
- and self.get_number_of_walls(x, y - 1) < 2:
- self.upper_walls_set_flags[x][y] = True
- self.start_walls -= 1
- else:
- if not self.left_walls_set_flags[x][y] \
- and self.get_number_of_walls(x, y) < 2 \
- and self.get_number_of_walls(x - 1, y) < 2:
- self.left_walls_set_flags[x][y] = True
- self.start_walls -= 1
-
- tries += 1
-
- # now it's the first players turn
- self.turn = "A"
- self.show()
-
- while True:
- # go through all events and check the types
- for event in pygame.event.get():
- # quit the game when the player closes it
- if event.type == pygame.QUIT:
- pygame.quit()
- exit(0)
-
- # left click
- elif event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[0]:
- if not self.accept_clicks:
- continue
-
- # get the current position of the cursor
- x = pygame.mouse.get_pos()[0]
- y = pygame.mouse.get_pos()[1]
-
- # check whether it was a not set wall that was clicked
- wall_x, wall_y = self.get_wall(x, y)
-
- if not (wall_x >= 0 and wall_y >= 0):
- continue
-
- upper_wall = wall_y % 30 == 0
-
- if upper_wall:
- if not self.upper_walls_set_flags[wall_x//30][wall_y//30]:
- self.upper_walls_set_flags[wall_x//30][wall_y//30] = True
- self.screen.blit(self.lineX, (wall_x, wall_y))
- else:
- continue
- else:
- if not self.left_walls_set_flags[wall_x//30][wall_y//30]:
- self.left_walls_set_flags[wall_x//30][wall_y//30] = True
- self.screen.blit(self.lineY, (wall_x, wall_y))
- else:
- continue
-
- if not self.set_all_slots() > 0:
- if self.turn == "A":
- self.turn = "B"
- elif self.turn == "B":
- self.turn = "A"
-
- if self.won():
- self.accept_clicks = False
-
- else:
-
- # set the display caption
- pygame.display.set_caption(self.turn + self.caption + " A:" + str(
- self.a_boxes) + " B:" + str(self.b_boxes))
-
- # update the players screen
- pygame.display.flip()
-
- def get_number_of_walls(self, slot_column, slot_row):
- """
- Get the number of set walls around the passed slot
- :param slot_column: x of the slot
- :param slot_row: y of the slot
- :return: number of set walls
- """
- number_of_walls = 0
-
- if slot_column == self.grid_size - 1:
- number_of_walls += 1
- elif self.left_walls_set_flags[slot_column + 1][slot_row]:
- number_of_walls += 1
-
- if slot_row == self.grid_size - 1:
- number_of_walls += 1
- elif self.upper_walls_set_flags[slot_column][slot_row + 1]:
- number_of_walls += 1
-
- if self.left_walls_set_flags[slot_column][slot_row]:
- number_of_walls += 1
-
- if self.upper_walls_set_flags[slot_column][slot_row]:
- number_of_walls += 1
-
- return number_of_walls
-
- @staticmethod
- def get_wall(pos_x, pos_y):
- rest_x = pos_x % 30
- rest_y = pos_y % 30
-
- wall_slot_x = pos_x//30
- wall_slot_y = pos_y//30
-
- # in a corner
- if rest_x < 4 and rest_y < 4:
- return -1, -1
-
- if rest_x < 4:
- # is left wall of the slot
- return wall_slot_x*30, wall_slot_y*30 + 4
-
- if rest_y < 4:
- # is upper wall of the slot
- return wall_slot_x*30 + 4, wall_slot_y*30
-
- # inside the box => not a wall
- return -1, -1
-
- def set_all_slots(self):
- """
- Find all newly closed boxes and close them for the current player
- :return: number of closed boxes
- """
- to_return = 0
-
- for column_ in range(self.grid_size):
- for row_ in range(self.grid_size):
- if self.grid_status[column_][row_] != 0 or self.get_number_of_walls(column_, row_) < 4:
- continue
-
- if self.turn == "A":
- self.grid_status[column_][row_] = 1
- self.screen.blit(self.A, (column_ * 30 + 4, row_ * 30 + 4))
- self.a_boxes += 1
- elif self.turn == "B":
- self.grid_status[column_][row_] = 2
- self.screen.blit(self.B, (column_ * 30 + 4, row_ * 30 + 4))
- self.b_boxes += 1
- elif self.turn == "X":
- self.grid_status[column_][row_] = 3
- self.screen.blit(self.X, (column_ * 30 + 4, row_ * 30 + 4))
- self.x_boxes += 1
-
- to_return += 1
-
- return to_return
-
- def won(self):
- """
- Check whether the game was finished
- If so change the caption to display the winner
- :return: won or not
- """
- if self.a_boxes + self.b_boxes + self.x_boxes == self.grid_size ** 2:
- if self.a_boxes < self.b_boxes:
- won_caption = "Player B won! Congrats"
- elif self.b_boxes < self.a_boxes:
- won_caption = "Player A won! Congrats"
- else:
- won_caption = "It's a tie!"
-
- # set the display caption
- pygame.display.set_caption(won_caption)
-
- # update the players screen
- pygame.display.flip()
-
- return True
- else:
- return False
-
- def show(self):
- """
- Reload the screen
- Use the current grid and wall information to
- update the players screen
- """
- self.screen.fill(0)
-
- # loop over all slots
- for column in range(self.grid_size):
- for row in range(self.grid_size):
- x, y = column * 30, row * 30
- self.screen.blit(self.block, (x, y))
- x += 4
- if not self.upper_walls_set_flags[column][row]:
- self.screen.blit(self.lineXempty, (x, y))
- else:
- self.screen.blit(self.lineX, (x, y))
- x -= 4
- y += 4
- if not self.left_walls_set_flags[column][row]:
- self.screen.blit(self.lineYempty, (x, y))
- else:
- self.screen.blit(self.lineY, (x, y))
-
- # calculate x and y in pixels
- x, y = column * 30 + 4, row * 30 + 4
-
- if self.grid_status[column][row] == 0:
- self.screen.blit(self.empty, (x, y))
- elif self.grid_status[column][row] == 1:
- self.screen.blit(self.A, (x, y))
- elif self.grid_status[column][row] == 2:
- self.screen.blit(self.B, (x, y))
- elif self.grid_status[column][row] == 3:
- self.screen.blit(self.X, (x, y))
-
- pygame.display.set_caption(self.turn + self.caption + " A:" + str(self.a_boxes) + " B:" + str(
- self.b_boxes))
- pygame.display.flip()
-
-
-game = Game() # start a game \ No newline at end of file
diff --git a/sandbox/pics/A.png b/sandbox/pics/A.png
deleted file mode 100644
index 7d04d60..0000000
--- a/sandbox/pics/A.png
+++ /dev/null
Binary files differ
diff --git a/sandbox/pics/B.png b/sandbox/pics/B.png
deleted file mode 100644
index c9f6ec7..0000000
--- a/sandbox/pics/B.png
+++ /dev/null
Binary files differ
diff --git a/sandbox/pics/X.png b/sandbox/pics/X.png
deleted file mode 100644
index 64bcf7c..0000000
--- a/sandbox/pics/X.png
+++ /dev/null
Binary files differ
diff --git a/sandbox/pics/block.png b/sandbox/pics/block.png
deleted file mode 100644
index 8c2f9d8..0000000
--- a/sandbox/pics/block.png
+++ /dev/null
Binary files differ
diff --git a/sandbox/pics/empty.png b/sandbox/pics/empty.png
deleted file mode 100644
index 8d3daad..0000000
--- a/sandbox/pics/empty.png
+++ /dev/null
Binary files differ
diff --git a/sandbox/pics/lineX.png b/sandbox/pics/lineX.png
deleted file mode 100644
index cec7bc0..0000000
--- a/sandbox/pics/lineX.png
+++ /dev/null
Binary files differ
diff --git a/sandbox/pics/lineXempty.png b/sandbox/pics/lineXempty.png
deleted file mode 100644
index ff09abe..0000000
--- a/sandbox/pics/lineXempty.png
+++ /dev/null
Binary files differ
diff --git a/sandbox/pics/lineY.png b/sandbox/pics/lineY.png
deleted file mode 100644
index 32fe351..0000000
--- a/sandbox/pics/lineY.png
+++ /dev/null
Binary files differ
diff --git a/sandbox/pics/lineYempty.png b/sandbox/pics/lineYempty.png
deleted file mode 100644
index ef10800..0000000
--- a/sandbox/pics/lineYempty.png
+++ /dev/null
Binary files differ