In C++, Create an interface so two players can take turns choosing from among 9
ID: 3891629 • Letter: I
Question
In C++, Create an interface so two players can take turns choosing from among 9 playing pieces ( (A,B,C,) (1,2,3) (a,b,c) ) to try and get 3 pieces of a particular kind in a row in a 3x3 board. Play alternates between the two players. Pieces can only be used once, and cannot be moved once played. Three in a row of a particular kind can be achieved by any of the following three possibilities: 1. Three of the same set, namely all three upper-case, all three lower-case, or all three numbers, in any order. Examples are: A B C, 1 3 2, c b a 2. Three of the same set position (first, second or third). In other words A is in the first position of the (A,B,C) set, and 2 is in the second position of the (1,2,3) set. Examples are: A 1 a, 2 b B, 3 C c 3. One of each set position (first, second or third). One value must be from the first position of one of the sets, a second value must be from the second position of one of the other sets, and the third value must be from the third position of the remaining set. Examples are: A 2 c, B 1 c, a, 3, B
Explanation / Answer
/*
The program takes row , column and a character(a/b/c/A/B/C/1/2/3)
row can take values from 1-3 and column can take values form 1-3
The input looks like 1 1 a after this input, the ouput is
a - -
- - -
- - -
*/
#include<iostream>
#include<string>
using namespace std;
int main(){
char board[3][3];
int a,b;
string win[] = {"ABC","ACB","BAC","BCA","CAB","CBA",
"123","132","213","231","312","321",
"abc","acb","bac","bca","cab","cba",
"A1a", "Aa1","1Aa","1aA","aA1","a1A",
"B2b", "Bb2","2Bb","2bB","bB2","b2B",
"C3c", "Cc3","3Cc","3cC","cC3","c3C",
"A2c", "Ac2","cA2","c2A","2cA","2Ac",
"B1c", "Bc1","cB1","c1B","1Bc","1cB",
"C2a", "Ca2","aC2","a2C","2Ca","2aC",
"a3B", "aB3","Ba3","B3a","3Ba","3aB"};
for (int i = 0; i<3; i++){
for (int j = 0; j<3; j++){
board[i][j] = '-';
}
}
int count = 0;
string choice = "ABCabc123";
string option = "ABCabc123";
string player ="Player1";
while(true){
if (count % 2 == 0)
player = "Player1";
else
player = "Player2";
for (int i = 0; i<3; i++){
for (int j = 0; j<3; j++){
cout << board[i][j] << " ";
}
cout << endl;
}
while(1){
cout << "Enter position(a(1/2/3),b(1/2/3) and choice("<< option << ")" << player << " :" << endl;
string ch;
cin >> a >> b >> ch;
//cout << a << " " << b << " " << ch << endl;
//break;
if (a > 3 || a < 1 || b > 3 || b < 1 ){
cout << "Invalid move ";
}
else {
int found = 0;
for (int i = 0; i<choice.length(); i++){
if (choice[i] == ch[0]){
found = 1;
break;
}
}
if (found == 0){
cout << "Invalid move ";
}
else {
found = 0;
for (int i = 0; i<option.length(); i++){
if (option[i] == ch[0]){
found = 1;
option[i] = '-';
break;
}
}
if (found == 0){
cout << "Invalid move ";
}
else {
if (board[a-1][b-1] == '-'){
board[a-1][b-1] = ch[0];
count++;
break;
}
else {
cout << "Invalid move ";
}
}
}
}
}
int full = 1;
for (int i = 0; i<3; i++){
for (int j = 0; j<3; j++){
if (board[i][j] == '-'){
full = 0;
break;
}
}
}
int winner = 0;
for (int i = 0; i<3; i++){
string win1 = "" + board[i][0] + board[i][1] + board[i][2];
for (int j = 0; j<60; j++){
if (win1 == win[j]){
winner = 1;
cout << player << " won ";
}
}
}
if (winner == 1)
break;
if (full == 1 && winner == 0){
cout << "It is a tie ";
break;
}
}
return 0;
}