We are planning to upgrade GitLab to the latest version this Friday morning. Expect some downtime!

...
 
Commits (2)
......@@ -19,4 +19,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
\ No newline at end of file
SOFTWARE.
......@@ -37,13 +37,17 @@ def new_game(n):
def add_tile(mat):
a = random.randint(0, len(mat)-1)
b = random.randint(0, len(mat)-1)
while mat[a][b] != 0:
if any([any([x == 0 for x in arr]) for arr in mat]):
a = random.randint(0, len(mat)-1)
b = random.randint(0, len(mat)-1)
mat[a][b] = 2 if random.random() < 0.9 else 4
return mat
while mat[a][b] != 0:
a = random.randint(0, len(mat)-1)
b = random.randint(0, len(mat)-1)
mat[a][b] = 2 if random.random() < 0.9 else 4
return mat
else:
return mat
###########
# Task 1c #
......@@ -97,6 +101,15 @@ def game_score(mat):
return int(score)
def total_value(mat):
score = 0
for i in range(c.GRID_LEN):
for j in range(c.GRID_LEN):
value = mat[i][j]
score += value
return int(score)
###########
# Task 2a #
###########
......
from enum import Enum, unique
import logic
from constants import *
import readchar
@unique
class Terminal(Enum):
Left = 1
Up = 2
Right = 3
Down = 4
class Player(object):
def __init__(self):
pass
def play(self, game):
raise NotImplementedError('Implement this in a subclass of Player!')
class DumbPlayer(object):
did_left = False
def play(self, game):
if self.did_left:
self.did_left = False
return Terminal.Down
else:
self.did_left = True
return Terminal.Left
class Automated2048(object):
state = None
def __init__(self):
pass
def play_game(self, player):
self._new_game()
loop_detected = False
while not loop_detected:
previous_score = logic.total_value(self.state)
move = player.play(self.state)
self.process_move(move)
self.state = logic.add_tile(self.state)
new_score = logic.total_value(self.state)
loop_detected = new_score == previous_score # If there is no increase in the score, then the move did nothing
return logic.total_value(self.state)
def _new_game(self):
self.state = logic.new_game(GRID_LEN)
self.state = logic.add_tile(self.state)
self.state = logic.add_tile(self.state)
def process_move(self, move):
if move == Terminal.Left:
new_state, _ = logic.left(self.state)
elif move == Terminal.Up:
new_state, _ = logic.up(self.state)
elif move == Terminal.Right:
new_state, _ = logic.right(self.state)
elif move == Terminal.Down:
new_state, _ = logic.down(self.state)
self.state = new_state
def print_game(self):
for row in self.state:
row_line = ''
for value in row:
row_line += f' {value} '
print(row_line)
if __name__ == '__main__':
print("Let's play a game")
player = DumbPlayer()
automated_game = Automated2048()
score = automated_game.play_game(player)
print("You got a total value of {}".format(score))