• 个人简介

    #include using namespace std;

    int main() { // 初始化棋盘的九个格子 char slot1 = '1'; char slot2 = '2'; char slot3 = '3'; char slot4 = '4'; char slot5 = '5'; char slot6 = '6'; char slot7 = '7'; char slot8 = '8'; char slot9 = '9'; // 当前玩家,默认为玩家X char currentPlayer = 'X'; char mark; int choice; bool gameover = false; int cnt = 0;//记录目前下了几个子了 cout << "欢迎来到井字游戏!" << endl;

    while (!gameover) {
    	system("cls");
    	// 打印当前棋盘状态
    	cout << "-------------" << endl;
    	cout << "| " << slot1 << " | " << slot2 << " | " << slot3 << " |" << endl;
    	cout << "-------------" << endl;
    	cout << "| " << slot4 << " | " << slot5 << " | " << slot6 << " |" << endl;
    	cout << "-------------" << endl;
    	cout << "| " << slot7 << " | " << slot8 << " | " << slot9 << " |" << endl;
    	cout << "-------------" << endl;
    	if (cnt == 9) {
    		cout << "平局" << endl;
    		break;
    	}
    	// 玩家输入要下棋的位置
    	cout << "玩家 " << currentPlayer << ",请输入一个数字: ";
    	cin >> choice;
    
    	// 根据玩家选择更新棋盘状态
    	if (choice == 1) {
    		if (slot1 == 'X' || slot1 == 'O') {
    			cout << "无效移动。该位置已被占用。" << endl;
    			continue;
    		}
    		mark = currentPlayer;
    		slot1 = mark;
    		cnt++;
    	} else if (choice == 2) {
    		if (slot2 == 'X' || slot2 == 'O') {
    			cout << "无效移动。该位置已被占用。" << endl;
    			continue;
    		}
    		mark = currentPlayer;
    		slot2 = mark;
    		cnt++;
    	} else if (choice == 3) {
    		if (slot3 == 'X' || slot3 == 'O') {
    			cout << "无效移动。该位置已被占用。" << endl;
    			continue;
    		}
    		mark = currentPlayer;
    		slot3 = mark;
    		cnt++;
    	} else if (choice == 4) {
    		if (slot4 == 'X' || slot4 == 'O') {
    			cout << "无效移动。该位置已被占用。" << endl;
    			continue;
    		}
    		mark = currentPlayer;
    		slot4 = mark;
    		cnt++;
    	} else if (choice == 5) {
    		if (slot5 == 'X' || slot5 == 'O') {
    			cout << "无效移动。该位置已被占用。" << endl;
    			continue;
    		}
    		mark = currentPlayer;
    		slot5 = mark;
    		cnt++;
    	} else if (choice == 6) {
    		if (slot6 == 'X' || slot6 == 'O') {
    			cout << "无效移动。该位置已被占用。" << endl;
    			continue;
    		}
    		mark = currentPlayer;
    		slot6 = mark;
    		cnt++;
    	} else if (choice == 7) {
    		if (slot7 == 'X' || slot7 == 'O') {
    			cout << "无效移动。该位置已被占用。" << endl;
    			continue;
    		}
    		mark = currentPlayer;
    		slot7 = mark;
    		cnt++;
    	} else if (choice == 8) {
    		if (slot8 == 'X' || slot8 == 'O') {
    			cout << "无效移动。该位置已被占用。" << endl;
    			continue;
    		}
    		mark = currentPlayer;
    		slot8 = mark;
    		cnt++;
    	} else if (choice == 9) {
    		if (slot9 == 'X' || slot9 == 'O') {
    			cout << "无效移动。该位置已被占用。" << endl;
    			continue;
    		}
    		mark = currentPlayer;
    		slot9 = mark;
    		cnt++;
    	} else {
    		cout << "无效输入。请在1到9之间输入一个数字。" << endl;
    		continue;
    	}
    	// 检查是否有玩家获胜
    	if ((slot1 == mark && slot5 == mark && slot9 == mark) ||
    	        (slot3 == mark && slot5 == mark && slot7 == mark) ||
    	        (slot1 == mark && slot2 == mark && slot3 == mark) ||
    	        (slot4 == mark && slot5 == mark && slot6 == mark) ||
    	        (slot7 == mark && slot8 == mark && slot9 == mark) ||
    	        (slot1 == mark && slot4 == mark && slot7 == mark) ||
    	        (slot2 == mark && slot5 == mark && slot8 == mark) ||
    	        (slot3 == mark && slot6 == mark && slot9 == mark)) {
    		gameover = true;
    	}
    	// 打印最终棋盘状态和获胜信息
    	if (gameover) {
    		cout << "-------------" << endl;
    		cout << "| " << slot1 << " | " << slot2 << " | " << slot3 << " |" << endl;
    		cout << "-------------" << endl;
    		cout << "| " << slot4 << " | " << slot5 << " | " << slot6 << " |" << endl;
    		cout << "-------------" << endl;
    		cout << "| " << slot7 << " | " << slot8 << " | " << slot9 << " |" << endl;
    		cout << "-------------" << endl;
    		cout << "玩家 " << currentPlayer << " 获胜!" << endl;
    	} else {
    		//切换玩家
    		if (currentPlayer == 'X') {
    			currentPlayer = 'O';
    		} else {
    			currentPlayer = 'X';
    		}
    	}
    }
    system("pause");
    return 0;
    

    }

  • 最近活动