Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <conio.h>
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <time.h>
- #include <windows.h>
- using namespace std;
- char k;
- int p=0;
- vector<string> map(9, "");
- string chess;
- void draw() {
- system("cls"); //cls means clear screen
- for( int i = 0 ; i <9 ; i++ ){
- if(map[i] != ""){
- cout << map[i];
- }
- else if(i ==p){
- cout << "□";
- }
- else{
- cout << "■";
- }
- if( i % 3 == 2 ){
- cout << endl;
- }
- }
- }
- int main() {
- chess="X"; //XO全形字元,不然棋盤會歪掉,shift+space可以切換全形半形
- draw();
- string whowin="";
- while (1) {
- if (kbhit()) {
- k=_getch();
- if( k == 'q' ){
- break;
- }
- else if( k == 'e' ){
- p = p-3;
- }
- else if( k == 'd' ){
- p = p+3;
- }
- else if( k == 's' ){
- p = p-1;
- }
- else if( k == 'f' ){
- p = p+1;
- }
- else if( k == 'c' ){
- if( map[p] == "" ){
- map[p] = chess;
- if( chess == "X" ){
- chess = "O";
- }
- else{
- chess = "X";
- }
- }
- }
- for( int i = 0 ; i <=6 ; i+=3 ){
- if( map[i] != "" && map[i] == map[i+1] && map[i] == map[i+2] ){
- whowin = map[i];
- }
- }
- for( int i = 0 ; i <=2 ; i++ ){
- if( map[i] != "" && map[i] == map[i+3] && map[i] == map[i+6] ){
- whowin = map[i];
- }
- }
- if( map[0] != "" && map[0] == map[4] && map[0] == map[8] ){
- whowin = map[0];
- }
- if( map[2] != "" && map[2] == map[4] && map[2] == map[6] ){
- whowin = map[2];
- }
- draw();
- if( whowin != "" ){
- cout << whowin << "win";
- break;
- }
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment