c语言 星球战舰对战游戏

使用C语言编程实现的一款星球战舰对战游戏,玩家需要输入船的位置,系统会判断船只是否越界以及该位置是否存在其他船只。游戏通过坐标点进行交互,提供紧张刺激的对战体验。
摘要由CSDN通过智能技术生成
C语言星球对战
  1. 先输入船的位置,判断船有没有越界,或者此位置已经有船了
  2. 输入坐标点
    在这里插入图片描述
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>

#define BOARDROWS 9
#define BOARDCOLS 11
bool dumpComputer = false; //causes dump of computer board before game if true (for debugging)

// boards. 0 means empty, non-zero means ship is there, number is size, -tive means hit on a ship
int playerBoard[BOARDROWS+1][BOARDCOLS+1];
int computerBoard[BOARDROWS+1][BOARDCOLS+1];
int compShotBoard[BOARDROWS+1][BOARDCOLS+1];

void showBoard(int board[BOARDROWS+1][BOARDCOLS+1]);
//测试输入数据
int getRand(int lowval,int highval){
   
    return (rand()%(highval+1-lowval) + lowval);
}




int getShot(bool fromUser, int board[BOARDROWS+1][BOARDCOLS+1]){
   
    int row,col;
    
    while(1){
   
        if(fromUser){
   
            printf("Give a shot (row, col):");
            scanf("%d%d",&row,&col);
            if(col<1 || col > BOARDCOLS || row < 1 || row > BOARDROWS)
                printf("Invalid input\n");
            else  //valid input
                break;
        }
        else {
    //computer generated
            row=getRand(1,BOARDROWS);
            col=getRand(1,BOARDCOLS);
            printf("computer shot is [%d,%d]",row,col);//debug
            if(compShotBoard[row][col]==0) {
    //make sure haven't shot here before
                compShotBoard[row][col]=1; //valid shot
                break;
            }
        }
    } //will leave this loop with valid input
    if(board[row][col] != 0){
   
        if(board[row][col]>0)
            board[row][col]= -1*board
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值