Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int max_array =10;
- // fall til ad reikna move x sem kallar a sig sjalft ef illegal move
- void x_move(char tic_tac_toe[], char x_input) {
- cout << "X position: ";
- cin >> x_input;
- if (x_input =1) {
- if (tic_tac_toe[1]=49) {
- tic_tac_toe[1]='X';
- }
- else {
- cout << "Illegal move!" << endl;
- x_move(tic_tac_toe, x_input);
- }
- }
- else if (x_input =2) {
- if (tic_tac_toe[2]=50) {
- tic_tac_toe[2]='X';
- }
- else {
- cout << "Illegal move!" << endl;
- x_move(tic_tac_toe, x_input);
- }
- }
- else if (x_input =3) {
- if (tic_tac_toe[3]=51) {
- tic_tac_toe[3]='X';
- }
- else {
- cout << "Illegal move!" << endl;
- x_move(tic_tac_toe, x_input);
- }
- }
- else if (x_input =4) {
- if (tic_tac_toe[4]=52) {
- tic_tac_toe[4]='X';
- }
- else {
- cout << "Illegal move!" << endl;
- x_move(tic_tac_toe, x_input);
- }
- }
- else if (x_input =5) {
- if (tic_tac_toe[5]=53) {
- tic_tac_toe[5]='X';
- }
- else {
- cout << "Illegal move!" << endl;
- x_move(tic_tac_toe, x_input);
- }
- }
- else if (x_input =6) {
- if (tic_tac_toe[6]=54) {
- tic_tac_toe[6]='X';
- }
- else {
- cout << "Illegal move!" << endl;
- x_move(tic_tac_toe, x_input);
- }
- }
- else if (x_input =7) {
- if (tic_tac_toe[7]=55) {
- tic_tac_toe[7]='X';
- }
- else {
- cout << "Illegal move!" << endl;
- x_move(tic_tac_toe, x_input);
- }
- }
- else if (x_input =8) {
- if (tic_tac_toe[8]=56) {
- tic_tac_toe[8]='X';
- }
- else {
- cout << "Illegal move!" << endl;
- x_move(tic_tac_toe, x_input);
- }
- }
- else if (x_input =9) {
- if (tic_tac_toe[9]=57) {
- tic_tac_toe[9]='X';
- }
- else {
- cout << "Illegal move!" << endl;
- x_move(tic_tac_toe, x_input);
- }
- }
- }
- // alveg eins fall fyrir O
- void o_move(char tic_tac_toe[], char o_input) {
- cout << "O position: ";
- cin >> o_input;
- if (o_input =1) {
- if (tic_tac_toe[1]=49) {
- tic_tac_toe[1]='O';
- }
- else {
- cout << "Illegal move!" << endl;
- o_move(tic_tac_toe, o_input);
- }
- }
- else if (o_input =2) {
- if (tic_tac_toe[2]=50) {
- tic_tac_toe[2]='O';
- }
- else {
- cout << "Illegal move!" << endl;
- o_move(tic_tac_toe, o_input);
- }
- }
- else if (o_input =3) {
- if (tic_tac_toe[3]=51) {
- tic_tac_toe[3]='O';
- }
- else {
- cout << "Illegal move!" << endl;
- o_move(tic_tac_toe, o_input);
- }
- }
- else if (o_input =4) {
- if (tic_tac_toe[4]=52) {
- tic_tac_toe[4]='O';
- }
- else {
- cout << "Illegal move!" << endl;
- o_move(tic_tac_toe, o_input);
- }
- }
- else if (o_input =5) {
- if (tic_tac_toe[5]=53) {
- tic_tac_toe[5]='O';
- }
- else {
- cout << "Illegal move!" << endl;
- o_move(tic_tac_toe, o_input);
- }
- }
- else if (o_input =6) {
- if (tic_tac_toe[6]=54) {
- tic_tac_toe[6]='O';
- }
- else {
- cout << "Illegal move!" << endl;
- o_move(tic_tac_toe, o_input);
- }
- }
- else if (o_input =7) {
- if (tic_tac_toe[7]=55) {
- tic_tac_toe[7]='O';
- }
- else {
- cout << "Illegal move!" << endl;
- o_move(tic_tac_toe, o_input);
- }
- }
- else if (o_input =8) {
- if (tic_tac_toe[8]=56) {
- tic_tac_toe[8]='O';
- }
- else {
- cout << "Illegal move!" << endl;
- o_move(tic_tac_toe, o_input);
- }
- }
- else if (o_input =9) {
- if (tic_tac_toe[9]=57) {
- tic_tac_toe[9]='O';
- }
- else {
- cout << "Illegal move!" << endl;
- o_move(tic_tac_toe, o_input);
- }
- }
- }
- //fall til ad tjekka hvort x se buinn ad vinna
- bool did_x_win (char tic_tac_toe[]) {
- if (tic_tac_toe[1]='X' && tic_tac_toe[2]='X' && tic_tac_toe[3]='X'){
- return true;
- }
- else if (tic_tac_toe[4]='X' && tic_tac_toe[5]='X' && tic_tac_toe[6]='X'){
- return true;
- }
- else if (tic_tac_toe[7]='X' && tic_tac_toe[8]='X' && tic_tac_toe[9]='X'){
- return true;
- }
- else if (tic_tac_toe[1]='X' && tic_tac_toe[4]='X' && tic_tac_toe[7]='X'){
- return true;
- }
- else if (tic_tac_toe[2]='X' && tic_tac_toe[5]='X' && tic_tac_toe[8]='X'){
- return true;
- }
- else if (tic_tac_toe[3]='X' && tic_tac_toe[6]='X' && tic_tac_toe[9]='X'){
- return true;
- }
- else if (tic_tac_toe[1]='X' && tic_tac_toe[5]='X' && tic_tac_toe[9]='X'){
- return true;
- }
- else if (tic_tac_toe[3]='X' && tic_tac_toe[5]='X' && tic_tac_toe[7]='X'){
- return true;
- }
- else {
- return false;
- }
- }
- // alveg eins fall fyrir O
- bool did_o_win (char tic_tac_toe[]) {
- if (tic_tac_toe[1]='O' && tic_tac_toe[2]='O' && tic_tac_toe[3]='O'){
- return true;
- }
- else if (tic_tac_toe[4]='O' && tic_tac_toe[5]='O' && tic_tac_toe[6]='O'){
- return true;
- }
- else if (tic_tac_toe[7]='O' && tic_tac_toe[8]='O' && tic_tac_toe[9]='O'){
- return true;
- }
- else if (tic_tac_toe[1]='O' && tic_tac_toe[4]='O' && tic_tac_toe[7]='O'){
- return true;
- }
- else if (tic_tac_toe[2]='O' && tic_tac_toe[5]='O' && tic_tac_toe[8]='O'){
- return true;
- }
- else if (tic_tac_toe[3]='O' && tic_tac_toe[6]='O' && tic_tac_toe[9]='O'){
- return true;
- }
- else if (tic_tac_toe[1]='O' && tic_tac_toe[5]='O' && tic_tac_toe[9]='O'){
- return true;
- }
- else if (tic_tac_toe[3]='O' && tic_tac_toe[5]='O' && tic_tac_toe[7]='O'){
- return true;
- }
- else {
- return false;
- }
- }
- // fall til ad prenta fylkid
- void print_board (char tic_tac_toe[]){
- cout << tic_tac_toe[1] << " " << tic_tac_toe[2] << " " << tic_tac_toe[3] << endl;
- cout << tic_tac_toe[4] << " " << tic_tac_toe[5] << " " << tic_tac_toe[6] << endl;
- cout << tic_tac_toe[7] << " " << tic_tac_toe[8] << " " << tic_tac_toe[9] << endl;
- }
- int main()
- {
- // búa til fylki af staerd 10 og fylla thad (hunsa 0)
- char tic_tac_toe[max_array];
- tic_tac_toe[1]=49;
- tic_tac_toe[2]=50;
- tic_tac_toe[3]=51;
- tic_tac_toe[4]=52;
- tic_tac_toe[5]=53;
- tic_tac_toe[6]=54;
- tic_tac_toe[7]=55;
- tic_tac_toe[8]=56;
- tic_tac_toe[9]=57;
- print_board(tic_tac_toe);
- //búa til bool gildi sem tjekkar hvort leikurinn sé búinn
- bool is_game_over = false;
- // prompta x fyrst og svo o, muna ad tjekka bool á milli og eftir o
- char x_input;
- char o_input;
- cout << tic_tac_toe[1] << " " << tic_tac_toe[2] << " " << tic_tac_toe[3] << endl;
- cout << tic_tac_toe[4] << " " << tic_tac_toe[5] << " " << tic_tac_toe[6] << endl;
- cout << tic_tac_toe[7] << " " << tic_tac_toe[8] << " " << tic_tac_toe[9] << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement