小y斗兽棋

这篇文章描述了一个编程问题,涉及两个人玩棋子游戏。每个玩家有四种棋子,每种棋子有不同的相互克制关系。程序需要根据玩家出的棋子判断胜负,返回特定的字符串结果。给出的代码实现了这个功能,通过比较棋子的相对位置来决定游戏的胜者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:https://ac.nowcoder.com/acm/contest/19306/1034
来源:牛客网

题目描述

牛牛喜欢牛妹,而喜欢的最高境界就是舔,牛牛作为一个舔狗,终于在不断的舔之中获得了牛妹的真心,但是牛牛还是没有舔舒服,于是他们今天又在玩游戏。

每个人有四个棋子,分别是“elephant”,“tiger”,“cat”,“mouse”,分别代表大象、老虎、猫、老鼠,(大象吃老虎,老虎吃猫,猫吃老鼠,老鼠吃大象),每次出一个棋子。

你能知道谁能获得胜利吗?

输入描述:
输入包含一行,两个以空格分隔的字符串分别代表牛牛和牛妹出的棋子,保证两个字符串一定在上述的四个字符串中。

输出描述:
如果牛牛赢了或者平局,输出“tiangou yiwusuoyou”,牛妹赢了输出“tiangou txdy”。

示例1
输入
tiger elephant
输出
tiangou txdy
备注:
如果牛牛出大象,牛妹出猫,那么我们认为这一局是平局,其他同理

解决方案

#include<stdio.h>
#include<string.h>
int func(char s[])
{
    char str[4][10]={"elephant","tiger","cat","mouse"};
    int i;
    for(i=0;i<4;i++)
    {
        if(strcmp(s,str[i])==0)
            break;
    }
    return i;//返回字符串的大小位置
}
int main()
{
    char s1[10],s2[10];
    scanf("%s",s1);
    scanf("%s",s2);
    int a=func(s1);//将字符大小比较转化为数字
    int b=func(s2);
    if(a-b==1||b-a==3)//从牛妹角度分析,两种结果
        printf("tiangou txdy\n");
    else
        printf("tiangou yiwusuoyou\n");
    return 0;
}
好的,我可以给你提供一个简单的C#代码示例来实现斗兽棋。 首先,我们需要定义一些枚举类型来表示棋子的种类和状态: ```csharp public enum AnimalType { None = 0, Lion = 1, Tiger = 2, Leopard = 3, Wolf = 4, Dog = 5, Cat = 6, Mouse = 7 } public enum AnimalStatus { Alive = 0, Dead = 1, Captured = 2 } ``` 接下来,我们定义一个Animal类来表示棋子: ```csharp public class Animal { public AnimalType Type { get; set; } public AnimalStatus Status { get; set; } public int X { get; set; } public int Y { get; set; } public bool IsEnemy { get; set; } public Animal() { Type = AnimalType.None; Status = AnimalStatus.Alive; X = -1; Y = -1; IsEnemy = false; } } ``` 然后,我们定义一个Board类来表示棋盘: ```csharp public class Board { private Animal[,] board; public Board() { board = new Animal[7, 9]; for (int i = 0; i < 7; i++) { for (int j = 0; j < 9; j++) { board[i, j] = new Animal(); } } } public Animal GetAnimal(int x, int y) { if (x < 0 || x >= 7 || y < 0 || y >= 9) { return null; } return board[x, y]; } public void SetAnimal(int x, int y, Animal animal) { if (x < 0 || x >= 7 || y < 0 || y >= 9) { return; } board[x, y] = animal; } public void MoveAnimal(int x1, int y1, int x2, int y2) { Animal animal = GetAnimal(x1, y1); if (animal == null) { return; } SetAnimal(x1, y1, new Animal()); SetAnimal(x2, y2, animal); animal.X = x2; animal.Y = y2; } } ``` 最后,我们定义一个Game类来表示游戏本身: ```csharp public class Game { private Board board; private bool isEnemyTurn; public Game() { board = new Board(); isEnemyTurn = false; Init(); } public void Init() { // 初始化棋子位置 // ... } public bool IsEnemyTurn() { return isEnemyTurn; } public void Move(int x1, int y1, int x2, int y2) { Animal animal = board.GetAnimal(x1, y1); if (animal == null || animal.Type == AnimalType.None || animal.IsEnemy != isEnemyTurn) { return; } // 检查移动是否合法 // ... board.MoveAnimal(x1, y1, x2, y2); isEnemyTurn = !isEnemyTurn; } public bool IsGameOver() { // 判断胜负 // ... return false; } } ``` 这只是一个简单的示例代码,具体实现过程中还需要考虑到很多细节问题,比如UI交互、动画效果等。希望这个示例能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值