from random import randint
class Board:
def__init__(self):#mapmaking function, works (requires optimization)
self.score = 0
self.size = 4
n = self.size+1
self.board = [[0]*n for i in range(self.size+1)]
self.board[1][1] = 2
def move(self, direction):#function that moves map, incomplete
if direction == 'w':
self.upMove()
def check(self):#A function that checks where possible on the map, i.e. determining game over, completing
for i in range(1,self.size: # Check if it is near the same thing except for the square edge
for j in range(1,self.size):
if self.board[i][j] == self.board[i+1][j] or self.board[i][j] == self.board[i][j+1]:
return True
if self.board[i][self.size] == self.board[i+1][self.size] :
return True
for j in range(1,self.size):
if self.board[self.size][j] == self.board[self.size][j+1] :
return True
return False
def check0 (self): #Function that determines if the map has a zero
for i in range(1,self.size+1):
for j in range(1,self.size+1):
if self.board[i][j] == 0:
return True
return False
def combine(self,direction):#function combining numbers, incomplete
if direction == 'w':
for j in range(1,self.size+1):
for i in range(1,self.size+1):
if i == self.size or self.board[i][j] == 0:
pass
else :
if self.board[i][j] == self.board[i+1][j]:
self.board[i][j] = self.board[i][j]*2
self.board[i+1][j] = 0
self.score = self.score + self.board[i][j]
def show(self):#Function showing map and score, complete
for i in range(1,self.size+1):
print()
for j in range(1,self.size+1):
print(str(self.board[i][j]).center(4), end = ' ')
print()
print('\nCurrent Score:', self.score)
defspawn(self):#function to add 2 or 4 to the map at random, complete
x = randint(1,self.size)
y = randint(1,self.size)
if self.board[y][x]==0:
self.board[y][x] = randint(1,2)*2
else :
while self.board[y][x]!=0:
x = randint(1,self.size)
y = randint(1,self.size)
if self.board[y][x]==0:
self.board[y][x] = randint(1,2)*2
def upMove(self):
for j in range(1,self.size+1):
for i in range(2, self.size+1):
if self.board[i][j] != 0:
y = i
while(self.board[y-1][j] == 0 and y > 1):
y = y-1
if y == 1:
break
if y != i:
self.board[y][j] = self.board[i][j]
self.board[i][j] = 0
else :
pass
def inputCheck(letter):# function to check input value, completed
try:
['w','a','s','d'].index(letter)
except :
print('Please enter only the specified characters.')
return True
else :
return False
print('2048')
print('The operating key is wasd.')
nowboard = Board()
while True:
nowboard.show()
while True:
inputLetter = 'w'
if not inputCheck(inputLetter):
break
nowboard.move(inputLetter)
nowboard.combine(inputLetter)
nowboard.move(inputLetter)
if nowboard.check0() :
nowboard.spawn()
if nowboard.check() or nowboard.check0():
continue
else :
nowboard.show()
print('GAME OVER')
break
else:
if nowboard.check():
continue
else:
nowboard.show()
print('GAME OVER')
break
I'm making 2048 games with Python, and I'm using wasd as a direction key. To make a part of it, I made it repeat it infinitely only when I received w as an input. However, it outputs normally up to a few times (three to five times), but after that, it doesn't print out and still stops. I thought it was a problem caused by infinite repetition, so even if I enter it one by one, it doesn't work even if I enter it three to four times. There's no error message. Why is that?
I think .spawn() is the problem, but I can't find what's the problem.
python python3
As you said, there is a problem with the Spawn method.
Code does not work as desired due to the scope of the variable.
defspawn(self):#function that randomly adds 2 or 4 to the map, complete
x = randint(1,self.size) # 1
y = randint(1,self.size) # 1
if self.board[y][x]==0:
self.board[y][x] = randint(1,2)*2
else :
while self.board[y][x]!=0:
x = randint(1,self.size) # 2
y = randint(1,self.size) # 2
I think you're trying to change the x and y values declared at 1 from 2. In fact, x in 1 and x in 2 are not the same variable. See similar questions for a rough description.
Using nonlocal c
in similar questions is one way, but why don't you change the code like this?
The code you wrote has a duplicate (if self.board[y][x]==0: self.board[y][x]=randint(1,2)*2). If you trim this part a bit more
defspawn(self):#function to add 2 or 4 to the map at random, complete
while True:
x, y = randint(1, self.size), randint(1, self.size)
if self.board[y][x] == 0:
self.board[y][x] = randint(1, 2) * 2
break
It's going to be like this way.
© 2024 OneMinuteCode. All rights reserved.