C + + Tic Tac Play, DOS interface version

Source: Internet
Author: User

It is said that there is a guaranteed unbeaten algorithm, tomorrow to see the first to write a PVC version.

The topic, today bored write a tic chess game, by the way gradually let oneself accustomed to good code style, put up to learn for beginners.

Jzq2.cpp

/*n Chess Game PvP version, the DOS version of the board can be expanded, only need to adjust the detection conditions can be, other interfaces do not need to change. Non-aircraft battle type, PvP type; @author: Unparalleled @date:2014-5-25@version:1.0*/#include <iostream> #include <string> #define Invalidindex-1#define full-2#define OK 0#define WIN 1#define isin-3using namespace std;struct box{//Use box to represent each lattice int c on the board hess;//with a color to represent the pieces, black and Whiteint status;//0 on behalf of the lattice no pieces, 1 representatives already have a pawn};enum color{black,white};class chessboard{private: static const int maxrow=10;static const INT Maxcolumn=10;int row;int Column;int blackbox;//remaining checkerboard number, you can drop point box Arr[maxrow] [Maxcolumn];void setrow (int r) {row=r;}; void Setcol (int c) {column=c;}; int GetRow () Const{return row;}; int Getcol () const{return column;}; Public:chessboard (int r,int col) {if (r>maxrow| | Col>maxcolumn) {cerr<< "Checkerboard size out of range" <<endl;setrow (MaxRow); Setcol (Maxcolumn);} Else{setrow (R); Setcol (col);//int Rr=getrow ();//int Cc=getcol ();//blackbox=r*col;//initialization can drop points//Cannot set BlackBox here, What's the situation? }initialize (); creat ();} void Initialize () {int r=chessboard::getrow (); int col=chessboard::getcol (); blackbox=r*col;for (int i=0;i<r;i++) for (int j=0;j<col;j++) {arr[i][j].chess=-1;arr[i][j].status=0;}} Out of range, return invalidindex-1//has won, return to win 1//board full, return full-2//normal fall chess return OK 0//the point exists pawn return isin-3int insertchess (int i,int J,co LOR c)//Fall Chess//Only provide fall-chess interface {int r=chessboard::getrow (); int Col=chessboard::getcol (); if (i<0| | j<0| | i>=r| | J>=col) return invalidindex;//if (c!=black&&c!=white)//return invalidindex;if (arr[i][j].status==0) {// The chess piece falls into the board arr[i][j].chess=c;arr[i][j].status=1;//mark this grid flush ();//Refresh Blackbox--;if (Isgameover ()) return WIN; if (Isfull ()) return Full;return OK;} return ISIN;} Protected:void creat () {//Initialize checkerboard int r=chessboard::getrow (); int col=chessboard::getcol (); for (int i=0;i<r;i++) {for ( int j=0;j<col-1;j++) {cout<< "|";} cout<<endl;}}; void Flush () {//Redraw checkerboard System ("CLS"); int r=chessboard::getrow (); int col=chessboard::getcol (); for (int i=0;i<r;i++) { for (int j=0;j<col;j++) {if (white==arr[i][j].chess) cout<< "0"; else if (black==arr[i][j].chess) cout<< " * ";elsecout<<" "If (j!=col-1) cout<<" | ";} Cout<<endl;}} BOOL Isfull () const{//infers if the chessboard has been filled with a pawn return blackbox==0;}; BOOL IsEmpty () const{;//infers if the chessboard is empty return Blackbox==getrow () *getcol ();};/ /checked by the board itself is full. Or whether the game is over bool Isfinish () const{//Check if the board is full return isfull ();//If the board is full, the game is over};bool isgameover () const{int R=chessboard:: GetRow (); int col=chessboard::getcol (); int color=-1;for (int i=0;i<r;i++) {//check each row, whether it is connected in a row, if (arr[i][0].chess== black| | Arr[i][0].chess==white) color=arr[i][0].chess;//assumes that the first box in each row has content and is black| | whiteelsecontinue;//detects the next line for (int j=1;j<col;j++) {if (color==arr[i][j].chess)//Assuming that the following is the same as the first color if (col==j+1) {// Assume that the last and equal string colors;if (color==1) colors= "white"; elsecolors= "Black";//cout<<endl<<colors< < "is winner!" <<endl;cout<<endl<< "Congratulations" <<colors<< "won the game!" "<<endl;return true;} ELSE//assumes that it is not the last, and continues to be different than the continue;else//assumed color break;}} Check each column for (int i=0;i<col;i++) {//Check each column, whether it is connected in a row, if (arr[0][i].chess==black| | Arr[0][i].chess==white) color=arr[0][i].chess;//hypothesisThe first box in each column has content and is black| | whiteelsecontinue;//detects the next column for (int j=1;j<r;j++) {if (color==arr[j][i].chess)//Assuming that the following is the same as the first color if (r==j+1) {// Assume that the last and equal string colors;if (color==1) colors= "white"; elsecolors= "Black";//cout<<endl<<colors< < "is winner!" <<endl;cout<<endl<< "Congratulations" <<colors<< "won the game!" "<<endl;return true;} ELSE//assumes that it is not the last, and continues to be different than the continue;else//assumed color break;}} Check positive diagonal color=arr[0][0].chess;bool falg=false;if (color==black| | Color==white)//Whether the first lattice has a pawn falg=true;if (FALG)//Assume that there is a pawn for (int i=1,j=1;i<r&&j<col;i++,j++) {if (Arr[i][j]. Chess==color) if (i==r-1) {string colors;if (color==1) colors= "white"; elsecolors= "Black";//cout<<endl<< colors<< "is winner!" <<endl;cout<<endl<< "Congratulations" <<colors<< "won the game!" "<<endl;return true;} Elsecontinue;elsebreak;} Measured sideways diagonal xcolor=arr[r-1][0].chess;falg=false;if (color==black| | Color==white)//Whether the first lattice has a pawn falg=true;if (FALG)//Assume that there is a pawn for (int i=r-2,j=1;i>=0&&j<col;i--, J + +) {if (aRr[i][j].chess==color) if (i==0) {string colors;if (color==1) colors= "white"; elsecolors= "Black";//cout<<endl <<colors<< "is winner!" <<endl;cout<<endl<< "Congratulations" <<colors<< "won the game!" "<<endl;return true;} Elsecontinue;elsebreak;} Return false;//assumptions are not satisfied, indicating that the game is not over};};
Main.cpp

#include <iostream> #include "jzq2.cpp" using namespace Std;int Main () {//3,3 represents the chessboard as 3*3, and refers to three rows of victories//identical, 5, 5 for 5-character chess , but the chessboard size is also 5*5//expansion board will be launched in the next version of Chessboard CB (3,3); int status; COLOR c=black;//records the next turn to who goes int x,y;bool falg=false;//used to record whether a successful fall of the bool isexit=false;//is used to record whether the game is over while (!isexit) {cout< < "\n\" 0\ "for White,\" *\ "for Black" <<endl;cout<< "Please enter a point of fall: for example, enter: 1 1" <<endl;string colors;if (c= =black) colors= "Black"; elsecolors= "White";cout<< "now turn to" <<colors<< "take the Next step:";cin>>x>> Y;/*if (FALG) c=c==black?white:black;//substitution walk */status=cb.insertchess (x,y,c); switch (status) {//out of range, return invalidindex-1/ /already won, return to win 1//board full, return full-2//normal fall chess return OK 0case 0:falg=true;c=c==black?white:black;//assume the successful fall chess, the substitution moves the next move break;case-1:cout << "The input coordinates are incorrect, out of range" <<endl;falg=false;break;case 1:cout<< "\ n \ nthe game is over! "<<endl;falg=false;isExit=true;break;case-2:cout<<" \ n \ nthe board is full! "<<endl;cout<<" \ n \ nthe game is coming to an end! "<<endl;falg=false;isExit=true;break;case-3:cout<<"\ n \ nthe point already has a pawn "<<endl;falg=false;break;}} Cin.get (); Cin.get ();};

Have tested the 3*3 of the bug, of course, if you enter a number. If you enter a letter, shoot it!

First put it in PvP to play, hahaha.

Today ran to the multi-benefit network machine test, back on the way unexpectedly remembered to the last problem write a deduction, depressed.

The other way back was probably thought out.

Depressed ing ...

All right, sleep, everybody, good night.

C + + Tic Tac Play, DOS interface version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.