Lab: Dodge Ball

Lab: Dodge Ball
Our game of Dodge Ball is a game for two Players: A and B, where players alternate moves, with A going first. A is given an n by n grid to fill out and B is given a length n row to fill out.

A fills in one of the upper rows of a grid with X’s and O’s
B fills in the leftmost square of the lower row with an X or an O
A fills in another one of the upper rows with X’s and O’s
B fills in the leftmost blank square of the lower row with an X or an O
this continues until B fills in all n columns of their row
Player A wins if one of their n rows matches the row of B. Player B wins if their row is different from all of Player A’s rows.

In class we saw that there is a winning perfect strategy for Player B. Your program should take as input a complete grid that was made by Player A and output the row that Player B would have made using our prefect strategy from class.

在这里插import java.util.Scanner;

public class Main
{
    private static boolean[] winningRow(boolean[][] player1sGrid)
    {
        boolean[]b1 = new boolean[player1sGrid.length];
        for(int n = 0; n<player1sGrid.length;n++){
            b1[n] = (!player1sGrid[n][n]);
        }
        return b1;
    }

    // Do not edit methods below here
    public static void main(String[] args)
    {
        // Read Player 1's grid of X's and O's
        final boolean[][] grid = readGrid();

        // Print the the moves made by Player 2 to win
        printRow(winningRow(grid));
    }

    private static boolean[][] readGrid()
    {
        // Note that we are not checking for bad input
        final Scanner cin = new Scanner(System.in);
        final int n = cin.nextInt();
        final boolean[][] grid = new boolean[n][n];

        for(int rowNumber = 0; rowNumber < n; rowNumber++)
        {
            String line = cin.next();
            for(int columnNumber = 0; columnNumber < n; columnNumber++)
            {
                grid[rowNumber][columnNumber] = line.charAt(columnNumber) == 'X';
            }
        }

        return grid;
    }

    private static void printRow(boolean[] row)
    {
        for(boolean b : row)
        {
            System.out.print(b ? 'X' : 'O');
        }
    }
}入代码片
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Sure, here's an example of a simple dodge game in Python using the Pygame library: ```python import pygame import random # Initialize Pygame pygame.init() # Set up the game window WINDOW_WIDTH = 600 WINDOW_HEIGHT = 800 WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) pygame.display.set_caption("Dodge Game") # Set up the player PLAYER_WIDTH = 50 PLAYER_HEIGHT = 50 player_x = WINDOW_WIDTH//2 - PLAYER_WIDTH//2 player_y = WINDOW_HEIGHT - PLAYER_HEIGHT - 20 player_speed = 5 player_rect = pygame.Rect(player_x, player_y, PLAYER_WIDTH, PLAYER_HEIGHT) # Set up the enemy ENEMY_WIDTH = 50 ENEMY_HEIGHT = 50 enemy_x = random.randint(0, WINDOW_WIDTH - ENEMY_WIDTH) enemy_y = -ENEMY_HEIGHT enemy_speed = 3 enemy_rect = pygame.Rect(enemy_x, enemy_y, ENEMY_WIDTH, ENEMY_HEIGHT) # Set up the score score = 0 font = pygame.font.Font(None, 36) # Set up the game loop game_over = False clock = pygame.time.Clock() while not game_over: # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True # Move the player keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and player_x > 0: player_x -= player_speed elif keys[pygame.K_RIGHT] and player_x < WINDOW_WIDTH - PLAYER_WIDTH: player_x += player_speed player_rect.x = player_x # Move the enemy enemy_y += enemy_speed enemy_rect.y = enemy_y # Check for collision if player_rect.colliderect(enemy_rect): game_over = True # Check if enemy is off the screen if enemy_y > WINDOW_HEIGHT: enemy_x = random.randint(0, WINDOW_WIDTH - ENEMY_WIDTH) enemy_y = -ENEMY_HEIGHT enemy_rect.x = enemy_x enemy_rect.y = enemy_y score += 1 enemy_speed += 0.5 # Draw the game WINDOW.fill((255, 255, 255)) pygame.draw.rect(WINDOW, (0, 0, 255), player_rect) pygame.draw.rect(WINDOW, (255, 0, 0), enemy_rect) score_text = font.render("Score: " + str(score), True, (0, 0, 0)) WINDOW.blit(score_text, (10, 10)) pygame.display.update() # Control the game speed clock.tick(60) # Quit Pygame pygame.quit() ``` This code sets up a simple game where the player must dodge an enemy that falls from the top of the screen. The player can move left and right using the arrow keys. The game ends if the player collides with the enemy. The score increases every time the player dodges the enemy. ### 回答2: Python dodge是指使用Python编程语言来开发一个躲避障碍物的小游戏。在这个游戏中,玩家通过控制一个角色来躲避不断出现的障碍物,尽可能地存活下去并获得高分。 Python dodge的实现过程大致如下:首先,需要使用Python编写游戏的主程序,包括游戏的初始化、循环、事件处理等。其次,需要使用相应的库或框架来创建游戏窗口、绘制图形、处理用户输入等。常用的库包括Pygame等。 在游戏中,玩家通过键盘或鼠标控制角色的移动,以躲避不断出现的障碍物。障碍物可以是飞来飞去的敌人、掉落的炸弹或其他会造成伤害的物体。玩家需要躲开这些障碍物,避免被撞到或受到伤害。同时,游戏也可以设置一些道具,玩家可以吃到这些道具来增加分数或生命值。 游戏的难度可以根据玩家的能力进行调整,可以逐渐增加障碍物的速度、密度或其他参数,增加游戏的挑战性和乐趣性。玩家可以通过存活时间或吃到的道具数量来衡量自己的成绩,并与其他玩家进行比较。 总之,Python dodge是一个简单而有趣的小游戏,通过编写Python代码实现游戏的逻辑和交互,让玩家能够享受到挑战和娱乐的乐趣。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值