java入门--4111:判断游戏胜者-Who Is the Winner

基础的题目 学习了StringBuilder, 通过delete来清空它 学了Map的简单用法

import java.util.*;

public class Main {


    public static int onepart_count(String s) {
        boolean newpart = false;
        int cnt = 0;

        for (int i = 0; i< s.length(); ++i) {
            if (!newpart && s.charAt(i) == '1') {
                cnt += 1;
                newpart = true;
            } else if (newpart && s.charAt(i) == '0')
                newpart = false;
        }
        return cnt;
    }

    public static void main(String[] args) {
        Map<Character, String> m = new HashMap<Character, String>();
        m.put('0', "0000");
        m.put('1', "0001");
        m.put('2', "0010");
        m.put('3', "0011");
        m.put('4', "0100");
        m.put('5', "0101");
        m.put('6', "0110");
        m.put('7', "0111");
        m.put('8', "1000");
        m.put('9', "1001");
        m.put('a', "1010");
        m.put('b', "1011");
        m.put('c', "1100");
        m.put('d', "1101");
        m.put('e', "1110");
        m.put('f', "1111");


        Scanner scan = new Scanner(System.in);
        int rowcount = scan.nextInt();
        for (int i = 0; i < rowcount; ++i) {
            String alice = scan.next();
            String bob = scan.next();
            StringBuilder sb = new StringBuilder();
            for (int j = 2; j < alice.length(); ++j) {
                Character c = alice.charAt(j);
                sb.append(m.get(c));
            }
            int cnt_alice = onepart_count(sb.toString());
            sb.delete(0, sb.length());
            for (int j = 2; j < bob.length(); ++j) {
                Character c = bob.charAt(j);
                sb.append(m.get(c));
            }
            int cnt_bob = onepart_count(sb.toString());
            if (cnt_alice > cnt_bob) {
                System.out.println("Alice");
            } else if (cnt_alice == cnt_bob) {
                System.out.println("Tie");
            }
            else
                System.out.println("Bob");
        }

    }


}

 

转载于:https://www.cnblogs.com/kwliu/p/4727513.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于α-β剪枝算法的五子棋游戏代码示例(Python实现): ```python import random # 定义棋盘的大小 BOARD_SIZE = 15 # 定义棋子的类型 EMPTY = 0 BLACK = 1 WHITE = 2 # 定义棋子在棋盘上的表示方式 PIECE_STR = ['.', 'X', 'O'] # 定义棋盘 board = [[EMPTY for x in range(BOARD_SIZE)] for y in range(BOARD_SIZE)] # 定义α-β剪枝算法中的最大深度 MAX_DEPTH = 3 # 定义评估函数中的权重 SCORE_WEIGHTS = [0, 1, 10, 100, 1000, 10000] def print_board(): """打印棋盘""" print(" ", end="") for i in range(BOARD_SIZE): print("{:2d}".format(i), end="") print() for i in range(BOARD_SIZE): print("{:2d}".format(i), end="") for j in range(BOARD_SIZE): print(" " + PIECE_STR[board[i][j]], end="") print() def get_winner(): """获取胜者""" for x in range(BOARD_SIZE): for y in range(BOARD_SIZE): if board[x][y] == EMPTY: continue if y + 4 < BOARD_SIZE and \ board[x][y] == board[x][y + 1] == board[x][y + 2] == board[x][y + 3] == board[x][y + 4]: return board[x][y] if x + 4 < BOARD_SIZE and \ board[x][y] == board[x + 1][y] == board[x + 2][y] == board[x + 3][y] == board[x + 4][y]: return board[x][y] if x + 4 < BOARD_SIZE and y + 4 < BOARD_SIZE and \ board[x][y] == board[x + 1][y + 1] == board[x + 2][y + 2] == board[x + 3][y + 3] == board[x + 4][y + 4]: return board[x][y] if x + 4 < BOARD_SIZE and y - 4 >= 0 and \ board[x][y] == board[x + 1][y - 1] == board[x + 2][y - 2] == board[x + 3][y - 3] == board[x + 4][y - 4]: return board[x][y] return EMPTY def get_score(piece_type): """计算当前棋盘的得分""" score = 0 for x in range(BOARD_SIZE): for y in range(BOARD_SIZE): if board[x][y] != piece_type: continue for direction_x, direction_y in [(1, 0), (0, 1), (1, 1), (1, -1)]: cnt = 1 for i in range(1, 5): new_x, new_y = x + i * direction_x, y + i * direction_y if new_x >= BOARD_SIZE or new_y >= BOARD_SIZE or board[new_x][new_y] != piece_type: break cnt += 1 score += SCORE_WEIGHTS[cnt] return score def alpha_beta_pruning(piece_type, depth, alpha, beta): """α-β剪枝算法""" winner = get_winner() if winner == piece_type: return 1000000 if winner != EMPTY: return -1000000 if depth == MAX_DEPTH: return get_score(piece_type) - get_score(3 - piece_type) best_score = -float("inf") if piece_type == BLACK else float("inf") for x in range(BOARD_SIZE): for y in range(BOARD_SIZE): if board[x][y] != EMPTY: continue board[x][y] = piece_type score = alpha_beta_pruning(3 - piece_type, depth + 1, alpha, beta) board[x][y] = EMPTY if piece_type == BLACK: best_score = max(best_score, score) alpha = max(alpha, score) else: best_score = min(best_score, score) beta = min(beta, score) if alpha >= beta: return best_score return best_score def ai_make_move(): """AI下棋""" best_moves = [] best_score = -float("inf") for x in range(BOARD_SIZE): for y in range(BOARD_SIZE): if board[x][y] != EMPTY: continue board[x][y] = BLACK score = alpha_beta_pruning(WHITE, 1, -float("inf"), float("inf")) board[x][y] = EMPTY if score > best_score: best_score = score best_moves = [(x, y)] elif score == best_score: best_moves.append((x, y)) return random.choice(best_moves) if __name__ == "__main__": while True: print_board() winner = get_winner() if winner != EMPTY: print("游戏结束,胜者是:", PIECE_STR[winner]) break x, y = map(int, input("请下棋(x y):").split()) if board[x][y] != EMPTY: print("非法落子,请重新输入") continue board[x][y] = WHITE x, y = ai_make_move() print("AI下棋:", x, y) board[x][y] = BLACK ``` 这个代码实现了一个简单的五子棋游戏,使用了α-β剪枝算法来实现AI下棋。其中,`get_winner`函数用来判断胜负,`get_score`函数用来评估当前棋盘的得分,`alpha_beta_pruning`函数用来执行α-β剪枝算法,`ai_make_move`函数用来让AI下棋。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值