当我运行以下X&O游戏时,一切都会按计划进行。但是,当某人获胜并且游戏应该结束时,尽管它印有“游戏结束”的事实,但随后游戏继续进行。
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class Request2 extends Request1
{
public function __construct()
{
parent::__construct();
$this->rules[] = 'unique:products,product_no,NULL,id,user_id,' . auth()->user()->id';
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return $this->rules;
}
}
上面的这一部分是为了使游戏更有趣,并确定转数何时超过所需数量。
player1 = input("What is the first player's name? ")
player2 = input("What is the second player's name? ")
board = [1, 2, 3, 4, 5, 6, 7, 8, 9]
def game(player1, player2, board, i):
if i <= 9:
print('\n' + 'This is the current state of the board' + '\n' + str(board[0]) + '|' + str(board[1]) + '|' +
str(board[2]) + '\n' + str(board[3]) + '|' + str(board[4]) + '|' + str(board[5]) + '\n' +
str(board[6]) + '|' + str(board[7]) + '|' + str(board[8]))
actions(player1, player2, board, i)
print('\n' + 'This is the current state of the board' + '\n' + str(board[0]) + '|' + str(board[1]) + '|' +
str(board[2]) + '\n' + str(board[3]) + '|' + str(board[4]) + '|' + str(board[5]) + '\n' +
str(board[6]) + '|' + str(board[7]) + '|' + str(board[8]))
以上部分是主要的游戏逻辑。它需要玩家的输入,并使用它来更改棋盘状态。
def actions(player1, player2, board, j):
if j % 2 == 1:
print(player1 + "'s turn")
play = int(input('In which position do you want to play? ')) - 1
if board[play] == 'x' or board[play] == 'o':
print('This is an invalid play')
actions(player1, player2, board, j)
else:
board[play] = 'x'
j += 1
checking(player1, player2, board, j)
elif j % 2 == 0:
print(player2 + "'s turn")
play = int(input('In which position do you want to play? ')) - 1
if board[play] == 'x' or board[play] == 'o':
print('This is an invalid play')
actions(player1, player2, board, j)
else:
board[play] = 'o'
j += 1
checking(player1, player2, board, j)
上面的这一部分在每次游戏后使用棋盘的当前状态,并检查刚刚玩过的玩家是否赢了。
def checking(player1, player2, board, j):
for x in (0, 3, 6):
if board[x] == board[x + 1] == board[x + 2] == 'x':
print(player1 + ' wins!')
print('Game Over')
game(player1, player2, board, 10)
elif board[x] == board[x + 1] == board[x + 2] == 'o':
print(player2 + ' wins!')
print('Game Over')
game(player1, player2, board, 10)
else:
pass
for x in (0, 1, 2):
if board[x] == board[x + 3] == board[x + 6] == 'x':
print(player1 + ' wins!')
print('Game Over')
game(player1, player2, board, 10)
elif board[x] == board[x + 3] == board[x + 6] == 'o':
print(player2 + ' wins!')
print('Game Over')
game(player1, player2, board, 10)
else:
pass
if board[0] == board[4] == board[8] == 'x':
print(player1 + ' wins!')
print('Game Over')
game(player1, player2, board, 10)
elif board[0] == board[4] == board[8] == 'o':
print(player2 + ' wins!')
print('Game Over')
game(player1, player2, board, 10)
elif board[2] == board[4] == board[6] == 'x':
print(player1 + ' wins!')
print('Game Over')
game(player1, player2, board, 10)
elif board[2] == board[4] == board[6] == 'o':
print(player2 + ' wins!')
print('Game Over')
game(player1, player2, board, 10)
else:
game(player1, player2, board, j)
答案 0 :(得分:2)
打印不会结束您的游戏,它只会打印。
如果您想结束游戏,则需要退出游戏。
您要结束游戏的打印,然后再次调用游戏,使其重新开始。
您应该改成return "Game Over"
答案 1 :(得分:2)
似乎您的代码并没有真正的结局:
def game(player1, player2, board, i):
if i <= 9:
(current state)
actions(player1, player2, board, i)
因此,从本质上讲,如果您位于9圈之内,请显示当前状态并调用函数“ actions()”。但是,无法检查游戏是否结束,或者结束时该怎么办。您只是让它轮流使用而没有其他选择
您可以使用和else
语句来提高确定性,或者使用while - False
。