|
本帖最后由 xieyug2012 于 2015-9-14 08:54 编辑
板子收到后,先试试把那个五子棋的程序写到pmon里,看看中不?
- //根据龙芯SoC实验教程整理。pmon下的一个五子棋小游戏,以供自娱自乐。
- #include <stdio.h>
- #include <stdlib.h>
- /#include <pmon.h>
- #define row 9 //棋盘大小,能动态改变
- //------------
- //基本结构和功能函数
- struct wzqPiece {
- int pieceKind; // 1黑,0白,初始一1
- int posX;
- int posY;
- };
- typedef struct wzqPiece piece;
- static void onDraw(int[][row], int *); //输出画面,接口是:当前棋盘,当前下棋步数
- static void receive(piece*, int *); //接收玩家下子,接口是:已下子的棋子数
- //组指针,当前下棋步数
- static int whoIsWinner(int[][row], piece *); //判断输赢,接口是:当前棋盘,已
- //下子的棋子数组指针
- //命令函数主体
- //static int playwzq(int ac, char ** av) {
- static int playwzq(int ac, char av) {
- piece pieceOnBoard[(row * row) + 1]; //下标从1开始
- piece Lastpiece; //当前在棋盘上的最后下的棋子信息
- int board[row][row]; //当前棋盘
- ///===========================================page 35
- int step = 0; //下棋步数
- int winner = -1; //1为黑棋赢,0为白棋赢, -1为暂时没有赢家
- int i, j; //循环变量 //初始化游戏
- for (i = 0; i < row; i++)
- for (j = 0; j < row; j++)
- board[i][j] = -1;
- for (i = 0; i < ((row * row) + 1); i++) {
- pieceOnBoard[i].pieceKind = -1;
- pieceOnBoard[i].posX = -1;
- pieceOnBoard[i].posY = -1;
- }
- Lastpiece.pieceKind = -1;
- Lastpiece.posX = -1;
- Lastpiece.posY = -1;
- onDraw(board, &step);
- step = 1;
- //初始化完毕
- //当有赢家或是棋盘已满时游戏结束
- while (winner == -1 && step < ((row * row) + 1)) {
- receive(pieceOnBoard, &step), Lastpiece.posX = pieceOnBoard[step].posX;
- Lastpiece.posY = pieceOnBoard[step].posY, Lastpiece.pieceKind =
- pieceOnBoard[step].pieceKind;
- board[Lastpiece.posX][Lastpiece.posY] = pieceOnBoard[step].pieceKind;
- onDraw(board, &step);
- winner = whoIsWinner(board, &Lastpiece);
- step++;
- }
- //游戏结束给出结果
- if (winner == 1)
- printf("winner is Black Player\n");
- else if (winner == 0)
- printf("winner is White Player \n");
- else
- printf("theDraw\n");
- return 0;
- }
- /* ------------------
- 普通功能函数实现部分包括
- onDraw() :输出当前每部棋盘画面,与玩家交互
- receive〇:每次通过终端从玩家那里接收下个棋子位置信息,并将相关信息保存
- 到已下棋子数组中去. .
- whoIsWinner() :判断当前棋局是否有赢家.并返回判断结果*/
- static void onDraw(int drawBoard[][row], int * StepPtr) {
- int i, j;
- if ((*StepPtr) % 2 == 1)
- printf("\n下一步,白棋走\n");
- else
- printf("\n下一步,黑棋走\n");
- printf("");
- //------------------------------------------------
- for (i = 0; i < row; i++)
- printf("%d", i);
- for (i = 0; i < row; i++) {
- printf("\n");
- printf("%d", i);
- for (j = 0; j < row; j++) {
- if (drawBoard[i][j] == 1) //画黑棋
- printf("@");
- else if (drawBoard[i][j] == 0) //画白棋
- printf("o");
- else
- printf("+"); //画空格
- }
- }
- printf("\n");
- return;
- }
- static void receive(piece * pieceArray, int*StepPtr) {
- int CorrectPosIn = 0; //判断输入是否结東标志, 1 为合法输入结束输入
- piece temp;
- int i;
- char c;
- printf("请输入下子的横坐标和纵坐标,q退出\n");
- while (CorrectPosIn == 0) {
- temp.posX = getchar();
- temp.posY = getchar();
- if (temp.posX == 'q' || temp.posY == 'q') {
- printf("end the wzq game bye:) \n");
- exit(0);
- }
- if (temp.posX >= '0' && temp.posX <= 9 && temp.posY >= '0'
- && temp.posY <= 9) {
- temp.posX = atoi(&(temp.posX));
- temp.posY = atoi(&(temp.posY));
- } else {
- temp.posX = -1;
- temp.posY = -1;
- printf("enter 0-9 number. . . . . \n");
- continue;
- if (temp.posX
- < 0|| temp.posX>=row|| temp.posY< 0|| temp.posY>=row) {
- Printf("数值不正确,请重新输入. . . \n");
- continue;
- }
- CorrectPosIn = 1;
- for (i = 0; i <= *StepPtr; i++)
- if (pieceArray[i].posX == temp.posX
- && pieceArray[i].posY == temp.posY) {
- CorrectPosIn = 0, Printf("这位置上已经有棋子了,请重新输入. . .\n");
- //scanf("%d%d″,&(temp. posX),&(temp. posY) );
- break;
- }
- }
- temp.pieceKind = (*StepPtr) % 2;
- pieceArray[(*StepPtr)] = temp;
- return;
- }
- }
- static int whoIsWinner(int curBoard[][row], piece * LastpiecePtr) {
- int x, y;
- int count = 1;
- //检查行 .
- y = LastpiecePtr->posY;
- for (x = (LastpiecePtr->posX) - 1; x >= 0; x--) {
- if (curBoard[x][y] == LastpiecePtr->pieceKind)
- count++;
- else
- break;
- }
- for (x = (LastpiecePtr->posX) + 1; x < row; x++) {
- if (curBoard[x][y] == LastpiecePtr->pieceKind)
- count++;
- else
- break;
- }
- if (count == 5)
- return LastpiecePtr->pieceKind;
- else if (count > 5)
- //=============================================page 40
- return ((LastpiecePtr->pieceKind) + 1) % 2;
- count = 1;
- //检查列
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|