(score + opponent_score) % 7 == 0换一种想法
时间: 2024-04-03 13:31:55 浏览: 8
我们可以这样想,当(score + opponent_score)能被7整除时,也就是(score + opponent_score)是7的倍数,那么(score + opponent_score)的个位数必定是0。因为如果个位数不是0,那么(score + opponent_score) % 7的结果就不可能是0了。所以可以换一种表达方式:score和opponent_score的个位数之和是7的倍数。
相关问题
def play(strategy0, strategy1, goal=GOAL_SCORE): """Simulate a game and return the final scores of both players, with Player 0's score first, and Player 1's score second. A strategy is a function that takes two total scores as arguments (the current player's score, and the opponent's score), and returns a number of dice that the current player will roll this turn. strategy0: The strategy function for Player 0, who plays first. strategy1: The strategy function for Player 1, who plays second. """ who = 0 # Which player is about to take a turn, 0 (first) or 1 (second) score, opponent_score = 0, 0 "*** YOUR CODE HERE ***" while score < goal and opponent_score < goal: if who == 0: score += take_turn(strategy0(score, opponent_score), opponent_score, select_dice(score, opponent_score)) if can_swap(score, opponent_score): # score = opponent_score # opponent_score = score score, opponent_score = opponent_score, score who = other(0) else: opponent_score += take_turn(strategy1(opponent_score, score), score, select_dice(opponent_score, score)) if can_swap(opponent_score, score): # opponent_score = score # score = opponent_score opponent_score, score = score, opponent_score who = other(1) return score, opponent_score换一种写法
下面是一种使用更加简洁的写法:
```
def play(strategy0, strategy1, goal=GOAL_SCORE):
scores = [0, 0]
who = 0
while max(scores) < goal:
opponent_score = scores[1 - who]
num_rolls = strategy0(scores[who], opponent_score) if who == 0 else strategy1(scores[who], opponent_score)
scores[who] += take_turn(num_rolls, opponent_score, select_dice(scores[who], opponent_score))
if can_swap(scores[who], opponent_score):
scores[who], scores[1 - who] = scores[1 - who], scores[who]
who = 1 - who
return tuple(scores)
```
这个写法将玩家的得分放在一个列表中,使用了更加简洁的语法来交替玩家的回合,并且使用了 `1 - who` 来表示另一个玩家的位置。同时,也将 `take_turn` 函数的参数与 `strategy` 函数的参数分开了,让代码更加清晰易懂。
解释#define BOARD_SIZE 15 #define EMPTY 0 #define BLACK 1 #define WHITE 2 #define INF 999999 int score[2][6] = { {0, 100, 500, 800, 1500, INF}, {0, 50, 250, 500, 1000, INF} }; int evaluate(int board[BOARD_SIZE][BOARD_SIZE], int player) { int opponent = (player == BLACK) ? WHITE : BLACK; int score_player = 0, score_opponent = 0; int count_player[6] = {0}, count_opponent[6] = {0}; int i, j, k;
这段代码定义了一些常量和变量来评估围棋棋盘上的局面。其中:
- `BOARD_SIZE` 定义了棋盘的大小为 15*15。
- `EMPTY`、`BLACK` 和 `WHITE` 分别代表棋盘上的空位、黑子和白子。
- `INF` 定义了一个极大值,用于在评估时表示最优的情况。
- `score` 是一个二维数组,表示在不同的情况下,对应的得分。其中第一维代表玩家的身份,第二维代表棋子的数量。例如,`score[0][3]` 表示黑方有三个棋子时的得分。
- `evaluate` 函数用来评估当前局面的得分。它首先确定当前玩家和对手的身份,然后根据棋盘上不同的棋子数量,统计出当前玩家和对手的得分。最后将两者之差作为局面的得分返回。
在这段代码中,`evaluate` 函数没有考虑各种棋型和棋形的影响,只是简单地根据棋子的数量计算得分。因此,它只能作为一种简单的评估方法,而不能很好地评估复杂的围棋局面。