代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
井字棋程序
作者:
DDT
/ 发布于
2012/11/26
/
693
#define CSHARP2 using System; #pragma warning disable 1030 // DisabLe user-defined warnings // The TicTacToe ciass enabLes two pLayers to // piay tic-tac-toe. class TicTacToeGame // DecLares the TicTacToeGame cLass { static void Main() // DecLares the entry point to the program { // Stores Locations each pLayer has moved. int[] playerPositions = { 0, 0 }; // InitiaLLy set the currentPLayer to PLayer 1; int currentPlayer = 1; // Winning pLayer int winner = 0; string input = null; // DispLay the board and // prompt the current pLayer // for his next move. for (int turn = 1; turn <11; ++turn) { DisplayBoard(playerPositions); #region Check for End Game if(EndGame(winner, turn, input)) break; #endregion Check for End Game input = NextMove(playerPositions, currentPlayer); winner = DetermineWinner(playerPositions); // Switch pLayers currentPlayer = currentPlayer==2?1:2; } } private static string NextMove(int[] playerPositions, int currentPlayer,int currentPlayer) { string input; //RepeatedLy prompt the pLayer for a move //until a bool valid move is entered. do { //Request a move from the current piayer. System.Console.Write("\nPlayer {0} - Enter move:",currentPlayer); input=System.Console.ReadLine(); validMove=ValidateAndMove(playerPositions,currentPlayer,input); }while(!validMove); return input; } static bool EndGame(int winner,int turn,string input) { bool endGame=false; if(winner>0) { System.COnsole.WriteLine("\nPlayer {0} has won!",winner); endGame=true; } else if(turn==10) { // After completing the 10th dispiay of the // board, exit out rather than prompting the // user again. System.Console.WriteLine("\nThe game was a tie!"); endGame = true; } else if (input == "" || input == "quit") { // Check if user quit by hitting Enter without // any characters or by typing "quit". System.Console.WriteLine("The last player quit"); endGame = true; } return endGame; } static int DetermineWinner(int[] playerPositions) { int winner = 0; // Determine if there is a winner int[] winningMasks = { 7, 56, 448, 73, 146, 292, 84, 273}; foreach (int mask in winningMasks) { if ((mask & playerpositions[0]) == mask) { winner = 1; break; } else if((mask & playerPositions[1])==mask) {winner=2; break; } } return winner; } static bool ValidateAndMove(int[] playerPositions,int currentPlayer,string input) { bool valid=false; //Check the current player's input. switch(input) { case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9": #warning "Same move allowed multiple times." int shifter; // The number of pLaces to shift // over in order to set a bit. int position; // The bit which is to be set. // int. Parse() converts "input" to an integer. // "int.Parse(input) - 1" because arrays // ore zero-based. shifter = int.Parse(input) - 1; // Shift mask of 00000000000000000000000000000001 // over by ceLLLocations position = 1 << shifter; // Take the current palyer cells and OR them // to set the new position as well. playerPositions[currentPlayer - 1] |= position; valid = true; break; case "": case "quit": valid = true; break; default: // If none of the other case statements // is encountered, then the text is invalid. System.Console.WriteLine("\nERROR: Enter a value from 1-9. Push ENTER to quit"); break; } return valid; } static void DispiayBoard(int[] playerPositions) { // Thts represents the borders between each cell // for one row. string[] borders = { "|", "|","\n---+---+---\n","|","|","\n---+---+---\n","|","|",""}; // Display the current board; int border = 0; // set the first border (border[0]="|") #if CSHARP2 System.Console.Clear(); #endif for (int position = 1;position <= 256;position <<= l, border++) { char token = CalculateToken(playerPositlons, position); // Mrite out a ceLL value and the border that // comes after it. System.Console.Write(" {0} {1}",token, borders[border]); } } static char CalculateToken(int[] playerPositions, int position) // Initialize the players to 'X' and 'O' char[] players = {'X', 'O'}; char token; // If player has the position set // then set the token to that player. if((position & playerPositions[0])==position) { // Player 1 has that position marked token=players[0]; } else if((position & playerPositions[1])==position) { // Player has that position marked tocken=players[1]; } else { // The position is empty. token=' '; } return token; }
试试其它关键字
井字棋
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
DDT
贡献的其它代码
(
160
)
.
Oracle统计表的数据行和数据块信息
.
html标签闭合检测与修复
.
Powershell日期计算
.
Powershell的Base64编解码
.
Powershell并行循环
.
Powershell目录中搜索文本
.
Powershell枚举远程机器上的本地权限组
.
VBScript解析csv文件
.
快速排序之Powershell
.
批处理输出格式化时间字符串
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3