用C语言进行游戏编程-对于初学者(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/game-programming-in-c-for-beginners-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 17 分钟阅读 - 8455 个词 阅读量 0用C语言进行游戏编程-对于初学者(译文)
原文地址:https://www.codeproject.com/Articles/447332/Game-Programming-in-C-For-Beginners
原文作者:Shine Jacob (Enot)
译文由本站 robot-v1.0 翻译
前言
A brief introduction to game programming using C/C++.
使用C/C ++进行游戏编程的简要介绍.
This article is for anyone who is interested in game programming. I will take you through the basics of game programming. Here we are specifically focusing on the classic DOS games. I cover and give examples for: Determining Initial requirements, Developing an Interface, analyzing different elements of the game design in the program that we’re going to make, and developing logic for Gameplay.
本文适用于对游戏编程感兴趣的任何人.我将带您了解游戏编程的基础知识.在这里,我们专门针对经典的DOS游戏.我将介绍并提供以下示例:确定初始需求,开发接口,在我们将要编写的程序中分析游戏设计的不同元素以及为游戏玩法开发逻辑.
游戏编程(Game Programming)
在我们真正进入游戏编程之前,我们需要知道一些称为事件的东西.(Before we actually jump into game programming, we need to know something called event)驱动编程(driven programming).事件驱动的编程是指这种编程方式,其中应用程序的用户可以从几个选项中自由选择,而不必局限于与程序进行交互的预定顺序.游戏编程是事件驱动编程的一个常见示例.游戏是一个封闭的,即完整且自给自足的形式系统,代表了现实的子集.游戏是动作-反应或事件-响应的完美结合,其中每个响应都基于最近发生的事件.(. Event driven programming refers to that style of programming wherein the user of the application is free to choose from several options rather than be confined to a predetermined sequence of interactions with the program. Game programming is one common example of event driven programming. A game is a closed, i.e., complete and self sufficient formal system that represents a subset of reality. A game is a perfect combination of actions-reactions or event-responses where every response is based on the most-recently occurred event.)
游戏编程要素(Elements of Game Programming)
通常,计算机游戏具有五个要素:(In general, a computer game has five elements:)
- 图形(Graphics)
- 声音(Sound)
- 接口(Interface)
- 游戏玩法(Gameplay)
- 故事(Story)
图形(Graphics)
图形由显示的所有图像以及对其执行的任何效果组成.这包括3D对象,纹理,2D磁贴,2D全屏快照,全动态视频(FMV)以及播放器将看到的任何其他内容.(Graphics consists of any images that are displayed and any effects that are performed on them. This includes 3D objects, textures, 2D tiles, 2D full screen shots, Full Motion Video (FMV) and anything else that the player will see.)
声音(Sound)
声音包括游戏期间播放的任何音乐或声音效果.这包括开始音乐,CD音乐,MIDI或MOD音轨,Foley效果(环境声音)和声音效果.(Sound consists of any music or sound effects that are played during the game. This includes starting music, CD music, MIDI or MOD tracks, Foley effects (environment sounds), and sound effects.)
接口(Interface)
声音包括游戏期间播放的任何音乐或声音效果.这包括开始音乐,CD音乐,MIDI或MOD音轨,Foley效果(环境声音)和声音效果.(Sound consists of any music or sound effects that are played during the game. This includes starting music, CD music, MIDI or MOD tracks, Foley effects (environment sounds), and sound effects.)
游戏玩法(Gameplay)
它涵盖了游戏的乐趣,程度和可玩性的长度.(It encompasses how fun the game is, how immense it is, and the length of playability.)
故事(Story)
游戏的故事包括游戏开始之前的任何背景,玩家在游戏中或获胜时获得的所有信息以及他们了解游戏角色的任何信息.故事是游戏的要素.故事和游戏之间的区别在于,故事以不变(即固定)的顺序表示事实,而游戏则表示顺序的分支树,并允许玩家通过在每个分支点进行选择来创建自己的故事.(The game’s story includes any background before the game starts, all information the player gains during the game or when they win and any information they learn about character in the game. A story is an element of a game. The difference between a story and a game is that a story represents the facts in an immutable (i.e., fixed) sequence, while a game represents a branching tree of sequences and allows the player to create his own story by making choice at each branch point.) 尽管图形在游戏编程中起着重要作用,但在本文中,我们将不着重于游戏的图形和声音元素.我们将专注于通过基于文本的界面进行基本的游戏编程.(Though graphics plays an important role in game programming, in this article we’re not going to emphasize upon graphics and sound element of a game. We shall be concentrating at elementary game programming through text based interfaces.)
游戏设计顺序(Game Design Sequence)
由于游戏设计需要一个人去探索自己的艺术能力,因此它不能一步一步地制定.但是,某些技术步骤需要以一种或另一种方式遵循.(Since game design requires one to explore one’s artistic abilities, it cannot be formulated in a step by step process. However, there are certain technical steps that one needs to follow in one way or another.) 这些是:(These are:)
- 确定初始需求.(Determining Initial requirements.)
- 开发界面.(Develop Interface.)
- 开发界面.(Develop Interface.)
- 开发计分逻辑.(Develop Logic for Scoring points.) 我们将详细研究每个.(We will look at each of these in detail.) 接口是游戏编程的另一个非常重要的方面.接口是计算机和播放器之间的通信模式.就像任何人类语言一样,它是程序员必须挤过的渠道,他/她试图与其他玩家分享这种想法,思想和感觉.界面将决定可以做什么或不能做什么.接口由输入和输出组成.在开发接口时,程序员应开发(Interface is another very important aspect of game programming. The interface is the mode of communication between the computer and the player. Like any human language, it is the funnel through which the programmer must squeeze the avalanche of thoughts, ideas and feelings that he/she seeks to share with the fellow player. Interface will dictate what can or cannot be done. Interface is composed of input and output. While developing interface, the programmer should develop the)静态显示屏(static display screens)和(and)动态显示屏(dynamic display screen).(.)静态显示(Static display)是不受玩家行为(即玩家输入)影响的屏幕.的(is the screen which remains unaffected by the player’s actions i.e., the input by the player. The)动态显示(dynamic display)另一方面,屏幕是由玩家的动作即玩家的输入所支配的.(, on the other hand, is the screen which is governed by the player’s actions i.e., the input by the player.) 一些静态显示屏的示例是:(Examples of some static display screens are:)
游戏选择画面(Game selection screens)
游戏启动时玩家可以使用哪些选项?这描述了菜单上的选项,在屏幕上的显示方式和位置,玩家如何到达那里以及如何离开.(What options are available to the player on the game startup? This describes what options are on the menu, how and where it appears on what screen, how the player gets there, and how he gets out.)
游戏开始画面(Game start screen)
游戏开始时的屏幕是什么样的,启动参数是什么,角色在哪里等?屏幕上显示什么消息(如果有),在哪里?介绍音乐?等等.(What does the screen looks like at the beginning of the game, what are the startup parameters, where are the characters, etc? What messages, if any are on screen, and where? Intro music? Etc.) 由于动态屏幕根据播放器提供的输入而有所不同,因此其说明太多了,因此无法在此处列出.一些例子:(Since the dynamic screen vary as per the input given by the player, their descriptions are too many to be listed here. Some examples:)
信息画面(Message Screens)
在开发界面时,您还需要通过暗示玩家走在正确的轨道上,根据玩家的法律行为在屏幕上进行操作.另外,您需要在需要警告玩家的屏幕上进行操作,以防其执行非法的举动或行为.(While developing interfaces, you also need to work on screens in response to legal actions of the player, by intimating that he/she is on the right track. Also, you need to work on the screens required to warn the player in case he/she commits an illegal move or action.)
游戏结束讯息(End of game message)
这些屏幕包括消息和对以下问题的回答:玩家输了怎么办?玩家获胜时会发生什么?玩家获得高分会怎样?游戏结束后玩家会去哪里?他如何开始新游戏?(These screens include messages and responses to questions like: What happens when the player loses? What happens when the player wins? What happens when the player get the high score? Where does the player go when the game is over? How does he start a new game?) 此步骤涉及为游戏玩法开发适当的逻辑.这要求游戏程序员以程序代码的形式回答许多问题.这些问题包括:游戏如何进行?有哪些控件?游戏目标是什么?玩家将如何实现游戏目标?换句话说,我们必须说,由于游戏代表事件驱动的情况,因此游戏编程人员,即,您必须指定或编程包括以下内容的所有内容:(This step involves developing a proper logic for gameplay. This requires the game-programmer to answer many questions in the form of program-code. These questions include: How is game played? What are the controls? What is the Game Goal? How is the player going to achieve the game goal? etc. In other words, we must say that since game represents an event-driven situation, the game-programmer i.e., you must specify or program everything that includes:)
-
确定初始要求(Determining Initial requirements)在编写游戏程序时,选择游戏目标后,需要确定其初始要求.例如,要编写一个用于猜数字的游戏程序,您需要确定一种生成数字的方法,所涉及的玩家数量,允许给玩家的机会数量,计分方法等.在这里,我们的目的并不是要您是一位专业的游戏程序员,而我们更专注于为您提供编写简单或基本的游戏程序的想法.(While writing a game program, after selecting the goal-of-game, one needs to determine its initial requirements. For instance, to write a game program for guessing a number, you need to decide about a way to generate the number, number of players involved, number of chances allowed to the player, a scoring methodology etc. Here we are not aiming at making you a professional game programmer, rather we are concentrating more at giving you an idea of writing simple or elementary game programs.) 游戏概述:(General Description of Game:)游戏的总体描述涉及游戏的总体概述,其工作方式,在每个级别上发生的事情等.它描述了(The general description of a game involves the general overview of the game, how it works, what happens on each level, etc. It describes)所有部分(all parts)从玩家角度看游戏:(of the game from the player’s perspective:)
- 在开始演奏之前,他应该知道些什么.(What he’s supposed to know before he starts playing.)
- 他所看到的.(What he sees.)
- 他做的事.(What he does.)
- 他对所看到和所做的事情的预期反应.(His intended reaction to what he sees and does.)
-
开发界面(Develop Interface)
-
开发游戏逻辑(Develop Logic of Gameplay)
- 对用户/玩家行为的响应.(responses to the user/player’s action.)
- 对系统事件的响应.(responses to system events.)
- 游戏规则.(rules of the game.)
- 如果是两人游戏(如果计算机是玩家),则计算机的动作和动作.(if it’s a two player game (if the computer is a player), then the computer’s moves and actions.)
-
开发保持得分的逻辑(Develop Logic for Keeping Scores)用于计分目的的开发逻辑是用于游戏的开发逻辑的子集.为此,您必须首先确定游戏中要遵循的计分策略.您将决定允许的最大机会数,计分机制,是否与时间相关联,等等.在此阶段中,将计算里程碑事件,并相应地(积极地或消极地)进行评分.(Developing logic for the scoring purposes is a subset of developing logic for the game play. For this, you must first decide the scoring policy that you’re going to follow in your game. You’re going to decide the maximum number of chances allowed, the scoring mechanism, whether it is tied to time or not, etc. During this phase, the milestone events are worked out and accordingly scoring (positively or negatively) is carried out.)
游戏中的里程碑事件(Milestone Events in a Game)
每隔一段时间,玩家需要以某种方式获得奖励(或惩罚)以达到游戏中的这一点.每个发生特殊事件的地方都称为里程碑事件.有一个量规可以让玩家知道他在正确(或错误)的赛道上,并会鼓励(或劝阻)他继续前进.(Every once in a while, the player needs to be rewarded (or penalized) somehow for reaching that point in the game. Each of these places where something special happens is called a Milestone Event. There are a gauge to let the player know he’s on the right (or wrong) track, and will encourage (or discourage) him to keep going.)
现在我们已经讨论了游戏开发中的这些不同阶段,现在让我们不要开发一个简单的井字游戏.(Now that we have discussed these different phases in game-development, let us not develop a simple tic-tac-toe game.)
游戏概述:(General Description of the game:)
- 这是一个两人游戏,因此该程序使用两个人的名字并将O和X分配给他们.(It’s a two player game so the program takes the name of two players and assign O and X to the players.)
- 玩家依次进入自己选择的方块中输入他们的招式.(Players enter their move turn by turn, into the box they choose.)
- 程序需要确保没有框被覆盖.(Program needs to assure that no box is overwritten.)
- 如果玩家试图将自己的举动输入到其他玩家已经采取的行动中,机会就会转移到其他玩家.(If the player tries to enter his/her move into the box that’s already taken by the other player the the chance passes over to the other player.)
- 程序需要运行直到玩家获胜,想要退出游戏或直到没有剩余动作为止.(Program needs to run till a player wins, want to quit the game or until there are no moves left.)
- 玩家获胜后,程序将显示该消息,并询问玩家是否想再次玩.(After a player wins, program displays the message and will ask the player if he/she wants to play again.) 现在让我们在我们要编写的程序中分析游戏设计的不同元素.(Now let us analyze different elements of the game design in the program that we’re going to make.) 这是一个两人游戏,因此我们需要两个变量来存储他们的名字并运行一个循环,要求玩家轮流输入他们的移动回合.因此,我们需要另一个变量来存储回合,以查看哪个玩家进入了移动.以下是变量:(It’s a two player game, so we need two variables to store their names and run a loop to ask for the player to enter their move turn by turn. So we need another variable to store the turn to see which player is to enter the move. Here are the variables:)
char name[2][30]; //double dimensional array to store names of the player
int chance; //to store the chance, to track which player is to enter the move
我们需要一个功能,当玩家按下箭头键并按下Enter键在框内输入其移动时,便可以处理对框的导航.我们需要另一个变量来跟踪玩家正在移动的当前盒子.一个数组,用于存储播放器输入的值.所以这是变量:(We need a function to handle the navigation to the boxes when the player presses arrow keys and hit the Enter button to enter his move in the box. We need another variable to track the current box the player is on at the movement. An array to store the values entered by the player. So here are the variables:)
int box; //to track the current box the player is on at the moment
char a[3][3]; //array to hold the actual values that player enter while playing
int navigate(char a[3][3], int box, int player, int key);
// to handle key presses and update the current box the player is on
// and to enter the move in to the box when player presses Enter.
在此功能中,char(Here in this function, char) a[3][3]
是保存移动的数组.(is the array that holds the moves.) box
是玩家所在的盒子,并且(is the box the player was on, and) key
是按下的键.(is the key pressed.)
需要另一个变量来计算匝数.总共有9个箱子,但是回合数可能超过9个,因为如果玩家尝试将其移动输入到已经采取的箱子中,则机会会转移给其他玩家.(Another variable is required to count the number of turns. There are nine boxes in total however the number of turns maybe more than nine because if the player tries to enter his move into a box that’s already taken, then the chance passes over to the other player.)
int turns; // to count the number of chances
我们需要一个函数来将移动放到玩家选择的盒子中,并且需要确保我们不会覆盖盒子中的值:(We need a function to put the move into the box chosen by the player and we need to make sure that we don’t overwrite the value in a box:)
void putintobox(char a[3][3], char ch, int box);
这里(Here) a[3][3]
用于表示盒子,(is used to represent the boxes,) ch
是字符" O"或" X",并且(is the character ‘O' or ‘X', and) box
是要在其中输入值的框.现在我们如何知道要放入盒子的哪个字符?嗯,这个功能是由(is the box into which the value is to be entered. Now how would we know what character to put into the box? Well, this function is called by the) navigate
上面提到的功能.所以如果(function mentioned above. So if the) navigate
函数的调用方式如下:(function is called like this:) box = navigate(a[3][3],3,0,ENTER);
,则意味着玩家1(此处玩家1-0,玩家2由2表示)需要输入框3.(, then it means that player1(here player1-0, player2 is represented by 2) needs to enter into box 3. The) putintobox
函数检查是否选中了该框,然后将值输入到代表该框的数组中((function checks if the box is taken and enter the value in to the array that represents the boxes () a[3][3]
),并调用另一个函数(), and calls another function) showbox(char ch, int box)
在指定框中在屏幕上显示字符.(to show the character on screen in the specified box.)
checkforwin
检查玩家是否赢得了比赛,并且(checks if the player has won the game or not and) boxesleft
将检查是否所有框都已填充.我们将需要另一个变量来检查玩家是否要退出游戏,所以–(will check if all boxes are filled. We would need another variable to check if the player wants to quit the game so –) int quit;
.(.)
为了与用户进行交互,显示了许多消息.还告知玩家他是否赢得了比赛或是否平局.该程序还将询问玩家是否要再次玩.因此,在我们的程序中,消息将是:(In order to interact with the user, many messages are displayed. Also the player is told if he won the game or if it’s a draw. The program will also ask if the player wants to play again. So in our program, the messages would be:)
该程序的逻辑是运行一个while循环,直到玩家获胜,或者所有盒子都被填满,但没有人赢得比赛或用户退出时,循环运行.现在,在循环运行时,跟踪进入移动的机会的可变机会将更新.函数将检查用户按下了什么键(用户只能输入上,下,左,右或Enter键),然后将光标移动到指定的框,然后将分配给播放器的字符输入数组并显示在屏幕上.它还确保没有框被覆盖.如果用户尝试覆盖该框,则机会将其他玩家作为对输入错误举动的玩家的惩罚而传递给其他玩家.在程序结束时,询问用户是否要再次玩游戏.(The logic of this program is to run a while loop that runs till a player wins, or all the boxes are filled up but no one won the game or if the user wants to quit. Now while the loop is running, the variable chance that tracks whose chance is it to enter the move is updated. A function will check what was the key what pressed by the user (user can enter only up, down, left, right or enter key) and moves the cursor to the specified box and enter the character assigned to the player into the array and displays that on the screen. It also makes sure that no box is overwritten. It the user tries to overwrite the box, chance is passed on to the other player as a penalty to the player who entered the wrong move. At the end of the program, the user is asked if he/she wants to play the game again.)
-
初始要求(Initial requirements)
-
开发界面(Developing Interface)
- 询问玩家的名字.(Ask the name of the players.)
- 显示谁有机会进入移动.(Display whose chance is it to enter the move.)
- 显示玩家是否获胜或是否平局.(Display if a player wins or if it’s a draw.)
- 当玩家想要退出时显示一条消息.(Display a message when the player wants to quit.)
- 显示一条消息,询问玩家是否要再次玩.(Display a message asking if the player wants to play again.)
-
开发游戏逻辑(Developing logic for Gameplay) 这是我们需要的功能列表:(Here is the list of functions we would need:)
-
void showframe(int posx, int posy)
-此功能将在指定位置显示井字框架.(- This function will show the frame of the Tic Tac Toe on the specified position.) -
void showbox(int ch, int box)
-此功能将在指定的框中显示指定的字符.(- This function will show a specified character into the specified box.) -
void putintobox(char a[3][3], char ch, int box)
-此函数用于将字符写入数组,并将调用(- This function is used to write the character into the array and will call)showbox(ch,box)
.(.) -
void gotobox(int box)
-此功能会将光标移至指定的框.(- This function will take the cursor into the specified box.) -
int navigate(char a[3][3], int box, int player, int key)
-此功能将用于跟踪用户在按键盘上的箭头键时所在的文件夹号.这还将获得用户想要输入分配给他/她的字符的框号.(- This function will be used to track the box number the user is at while he is pressing the arrows keys on the keyboard. This will also get the box number in which the user wants to enter the character assigned to him/her.) -
int checkforwin(char a[3][3])
-检查玩家是否获胜.(- Checks if the player wins.) -
int boxesleft(char a[3][3])
-检查剩余多少个盒子.(- To check how many boxes are left.) 功能细节:(Details of the function:)void showframe(int posx, int posy)
//Function to show the Tic Tac Toe Frame
void showframe(int posx, int posy)
{
int hr=196, vr=179; // These are ascii character which display the lines
int crossbr=197; // Another ascii character
int x=posx, y=posy;
int i,j;
gotoxy(35,4); cprintf("TIC TAC TOE");
gotoxy(35,5); for(i=0;i<11;i++) cprintf("%c",223);
for(i=0;i<2;i++)
{
for(j=1;j<=11;j++)
{
gotoxy(x,y);
printf("%c",hr);
x++;p; x++;
}
x=posx; y+=2;
}
x=posx+3; y=posy-1;
for(i=0;i<2;i++)
{
for(j=1;j<=5;j++)
{
gotoxy(x,y);
printf("%c",vr);
y++;
}
x+=4;y=posy-1;
}
x=posx+3; y=posy;
gotoxy(x,y);
printf("%c",crossbr);
x=posx+7; y=posy;
gotoxy(x,y);
printf("%c",crossbr);
x=posx+3; y=posy+2;
gotoxy(x,y);
printf("%c",crossbr);
x=posx+7; y=posy+2;
gotoxy(x,y);
printf("%c",crossbr);
}
无效的显示框(char ch,int框)(void showbox(char ch, int box))
//Function to show the character in the specified box
void showbox(char ch, int box)
{
switch(box)
{
case 1 : gotoxy(_x+1,_y-1); printf("%c",ch); break; //1st box
case 2 : gotoxy(_x+5,_y-1); printf("%c",ch); break; //2nd box
case 3 : gotoxy(_x+9,_y-1); printf("%c",ch); break; //3rd box
case 4 : gotoxy(_x+1,_y+1); printf("%c",ch); break; //4th box
case 5 : gotoxy(_x+5,_y+1); printf("%c",ch); break; //5th box
case 6 : gotoxy(_x+9,_y+1); printf("%c",ch); break; //6th box
case 7 : gotoxy(_x+1,_y+3); printf("%c",ch); break; //7th box
case 8 : gotoxy(_x+5,_y+3); printf("%c",ch); break; //8th box
case 9 : gotoxy(_x+9,_y+3); printf("%c",ch); break; //9th box
}
}
无效的putintobox(字符a [3] [3],字符ch,整数框)(void putintobox(char a[3][3], char ch, int box))
//Function to insert the specified character into the array
void putintobox(char arr[3][3], char ch, int box)
{
switch(box)
{
case 1 : if(arr[0][0] != 'X' && arr[0][0]!= 'O')
{ arr[0][0] = ch;
showbox(ch,1);
}
break;
case 2 : if(arr[0][1] != 'X' && arr[0][1]!= 'O')
{ arr[0][1] = ch;
showbox(ch,2);
}
break;
case 3 : if(arr[0][2] != 'X' && arr[0][2]!= 'O')
{ arr[0][2] = ch;
showbox(ch,3);
}
break;
case 4 : if(arr[1][0] != 'X' && arr[1][0]!= 'O')
{ arr[1][0] = ch;
showbox(ch,4);
}
break;
case 5 : if(arr[1][1] != 'X' && arr[1][1]!= 'O')
{ arr[1][1] = ch;
showbox(ch,5);
}
break;
case 6 : if(arr[1][2] != 'X' && arr[1][2]!= 'O')
{ arr[1][2] = ch;
showbox(ch,6);
}
break;
case 7 : if(arr[2][0] != 'X' && arr[2][0]!= 'O')
{ arr[2][0] = ch;
showbox(ch,7);
}
break;
case 8 : if(arr[2][1] != 'X' && arr[2][1]!= 'O')
{ arr[2][1] = ch;
showbox(ch,8);
}
break;
case 9 : if(arr[2][2] != 'X' && arr[2][2]!= 'O')
{ arr[2][2] = ch;
showbox(ch,9);
}
break;
}//end of switch
}
无效的gotobox(int box)(void gotobox(int box))
//Function to show the curson on the box specified
//uses the position to check the coordinates
void gotobox(int box)
{
switch(box)
{
case 1 : gotoxy(_x+1,_y-1); break;
case 2 : gotoxy(_x+5,_y-1); break;
case 3 : gotoxy(_x+9,_y-1); break;
case 4 : gotoxy(_x+1,_y+1); break;
case 5 : gotoxy(_x+5,_y+1); break; //5th box
case 6 : gotoxy(_x+9,_y+1); break; //6th box
case 7 : gotoxy(_x+1,_y+3); break; //7th box
case 8 : gotoxy(_x+5,_y+3); break; //8th box
case 9 : gotoxy(_x+9,_y+3); break;
}
}
int导航(char a [3] [3],int框,int播放器,int键)(int navigate(char a[3][3], int box, int player, int key))
//Function to handle the navigation
int navigate(char arr[3][3], int box, int player, int key)
{
switch(key)
{
case UPARROW : if( (box!=1) || (box!=2) || (box!=3) )
{ box-=3; if(box<1) box = 1; gotobox(box); }
break;
case DOWNARROW : if( (box!=7) || (box!=8) || (box!=9) )
{ box+=3; if(box>9) box = 9; gotobox(box); }
break;
case LEFTARROW : if( (box!=1) || (box!=4) || (box!=7) )
{ box--; if(box<1) box = 1; gotobox(box); }
break;
case RIGHTARROW : if( (box!=3) || (box!=6) || (box!=9) )
{ box++; if(box>9) box = 9; gotobox(box); }
break;
case ENTER : if(player == 0)
putintobox(arr,'O',box);
else if(player == 1)
putintobox(arr,'X',box);
break;
}//end of switch(key)
return box;
}
int checkforwin(字符a [3] [3])(int checkforwin(char a[3][3]))
int checkforwin(char arr[3][3])
{
int w=0;
/* 0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,2
*/
//rows
if((arr[0][0] == arr[0][1]) && (arr[0][1] == arr[0][2])) w = 1;
else if((arr[1][0] == arr[1][1]) && (arr[1][1] == arr[1][2])) w = 1;
else if((arr[2][0] == arr[2][1]) && (arr[2][1] == arr[2][2])) w = 1;
//coloums
else if((arr[0][0] == arr[1][0]) && (arr[1][0] == arr[2][0])) w = 1;
else if((arr[0][1] == arr[1][1]) && (arr[1][1] == arr[2][1])) w = 1;
else if((arr[0][2] == arr[1][2]) && (arr[1][2] == arr[2][2])) w = 1;
//diagonal
else if((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2])) w = 1;
else if((arr[0][2] == arr[1][1]) && (arr[1][1] == arr[2][0])) w = 1;
return w;
}
int boxleft(char a [3] [3])(int boxesleft(char a[3][3]))
int boxesleft(char a[3][3])
{
int i,j,boxesleft=9;
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
{ if((a[i][j] == 'X') ||(a[i][j] == 'O'))
boxesleft--;
}
}
return boxesleft;
}
现在我们已经具备了所有功能,我们继续进行第三步(Now we have all the functions in place, we move on to the third step that talks about)介绍(Presentation).(.) 实际上,我们已经完成了上述某些书面功能中的演示,不是吗?(We have actually taken care of presentation in some of the above written functions, haven’t we?) 该程序中用于显示垂直线的ASCII字符为179,对于水平线的显示为196.对于十字,则为197.有关扩展ASCII字符的更多信息,请访问此网站.(The ASCII character used in this program to display the vertical line is 179 and for horizontal line is 196. For a cross - 197. Check this website for more information about extended ASCII characters) http://www.asciitable.com/(http://www.asciitable.com/) .(.) 您甚至可以使用C程序打印出ASCII表.这是一个例子:(You can even printout your ASCII table with a C program. Here is an example:)
#include <stdio.h>
int main()
{
FILE *fh;
int ch;
fh = fopen("ascii.txt","r");
for(i=0;i<256;i++)
fprint(fh,"\n%d - %c",i,i);
fclose(fh);
return 0;
}
现在我们将讨论(Now we will talk about)异常处理(Exception Handling).在此程序中,我们将其保持简单,因此在此无需过多担心,但是,我们确实从用户那里取了名字.我们用于存储名称的数组的大小为30.如果用户输入的字符串长度超过30,该怎么办?这将导致缓冲区溢出.这可能会使您的程序立即崩溃或产生意外结果.为了避免这种情况,我们可以编写一个函数,该函数将从(. Here in this program, we have kept it simple so nothing much to worry about here, however, we do take the name from the user. The array we use to store the name is of size 30. What if the user enters a string that is more than 30 in length? This would result in a buffer overflow. This may crash your program right away or produce unexpected results. To avoid this either we can write a function that would get a string from the) stdin
一个接一个地停止,如果按下Enter键或字符串大于30,或者我们可以使用称为(one by one and stops if Enter is pressed or if string is more than 30 OR we can use the inbuilt function known as) fgets
.(.)
最后,将所有内容放在一起:(Finally, putting it all together:)
#include <stdio.h>
#include <conio.h>
int main()
{
/*
Declaration of variables used
*/
showframe(12,25);
printf("\nPlayer 1, enter your name:"); fgets(name[0], 30, stdin);
printf("\nPlayer 2, enter your name:"); fgets(name[1], 30, stdin);
printf("\n%s, you take 0",name[0]);
printf("\n%s, you take X",name[1]); getch();
clrscr();
do
{
while(!enter)
{
if(khbit())
ch = getch();
switch(ch)
{
case UPARROW : box = navigate(a[3][3], box, player, UPARROW);
.
.
.
}
}
if(quit) break;
//check if the player wins
win = checkforwin(a);
}while(!win)
if(win)
{ .
.
}
else if(quit)
{ .
.
}
return 0;
}
查看完整的源代码和可执行文件,以查看程序的工作方式.(View the complete source code and executable to take a look at how the program works.)
这是井字游戏的可执行文件的一些屏幕截图:(Here are some screenshots of the working executable for Tic Tac Toe:)
本文不是完整的有关游戏编程的完整文章,但我希望您能从中学到一些东西.如有任何疑问,请随时发电子邮件至shine_hack@yahoo.com(This article was not a complete fully fledged article for game programming but I hope that you gain something out of it. If you have any questions, please feel free to email me at shine_hack@yahoo.com) 该程序的源代码可以在以下位置获得:(Source code for this program can be obtained at:) http://techstuff.x10.mx/articles/(http://techstuff.x10.mx/articles/) .(.) 编程愉快!(Happy programming!) 闪耀雅各布(Enot):shine_hack@yahoo.com(Shine Jacob (Enot): shine_hack@yahoo.com)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C Dev 新闻 翻译