Question

C++ I am implementing a simple version of game 2048, which is in 3x3 grid, and...

C++

I am implementing a simple version of game 2048, which is in 3x3 grid, and the player wins if 32 appears, and the new tile will be either 2 or 4.

I have no idea how to complete the following two fuction,

for the first one, randomly generate 2 or 4 to an avaliable tile   

for the second one, the directions are w,a,s and d.

void putTile(int &n1, int &n2, int &n3, int &n4, int &n5, int &n6, int &n7, int &n8, int &n9)
{
        // TODO: randomly put 2 or 4 to one available tile
}
void slidingTiles(char direction, int& n1, int& n2, int& n3, int& n4, int& n5, int& n6, int& n7, int& n8, int& n9)
{
        // TODO: slide the nine tiles based on direction
}

i dont know how to compelte the function
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <iomanip>
using namespace std;

// function prototypes
void putTile(int& n1, int& n2, int& n3, int& n4, int& n5, int& n6, int& n7, int& n8, int& n9);
void slidingTiles(char direction, int& n1, int& n2, int& n3, int& n4, int& n5, int& n6, int& n7, int& n8, int& n9);

void print(int& n1, int& n2, int& n3, int& n4, int& n5, int& n6, int& n7, int& n8, int& n9);

int main()
{
   // first define 9 numbers to represent 3by3 tiles
   // using array would be easier!
   int n1 = 0, n2 = 0, n3 = 0, n4 = 0, n5 = 0, n6 = 0, n7 = 0, n8 = 0, n9 = 0;

   // initialize game board by putting tiles
   putTile(n1, n2, n3, n4, n5, n6, n7, n8, n9);

   // play the game in loop
   while (true) {
       // first check if there are empty tiles
       if (n1 != 0 && n2 != 0 && n3 != 0 && n4 != 0 && n5 != 0 && n6 != 0 && n7 != 0 && n8 != 0 && n9 != 0) {
           // no place to put new tile game over!
           cout << "GAME OVER" << endl;
           break;
       }
       // next put a tile on board
       putTile(n1, n2, n3, n4, n5, n6, n7, n8, n9);
       // print the board
       print(n1, n2, n3, n4, n5, n6, n7, n8, n9);
       // ask user to make a move
       char dir;
       while (true) { // keep asking till user make a valid move
           cout << "Enter a direction to move tiles " << endl;
           cout << "W = up, S = down, A = left, D = right" << endl;
           cin >> dir;
           if (dir && dir == 'w' || dir == 'W' || dir == 's' || dir == 'S' || dir == 'a' || dir == 'A' || dir == 'd' || dir == 'D') {
               // move tiles in given direction
               slidingTiles(dir, n1, n2, n3, n4, n5, n6, n7, n8, n9);
               break;
           }
           else {
               cout << "Invalid direction!" << endl;
           }
       }
       // check for winning condition
       if (n1 == 32 || n2 == 32 || n3 == 32 || n4 == 32 || n5 == 32 || n6 == 32 || n7 == 32 || n8 == 32 || n9 == 32) {
           // print the board
           print(n1, n2, n3, n4, n5, n6, n7, n8, n9);
           cout << "YOU WON !" << endl;
           break;
       }
   }

}

// function definition
void putTile(int& n1, int& n2, int& n3, int& n4, int& n5, int& n6, int& n7, int& n8, int& n9)
{
   // parameter are passed as reference so any change in parameter here in this function
   // reflect in value of that parameter in main function
   // generate a random number from 1 to 9 to put tile in random place
   int place;
   int num = 2 + ((rand() % 2) * 2); // generate random 2 or 4
  
   while (true) {
       place = (rand() % 9) + 1;
       if (place == 1 && n1 == 0) {
           n1 = num;
           break;
       }
       else if (place == 2 && n2 == 0) {
           n2 = num;
           break;
       }
       else if (place == 3 && n3 == 0) {
           n3 = num;
           break;
       }
       else if (place == 4 && n4 == 0) {
           n4 = num;
           break;
       }
       else if (place == 5 && n5 == 0) {
           n5 = num;
           break;
       }
       else if (place == 6 && n6 == 0) {
           n6 = num;
           break;
       }
       else if (place == 7 && n7 == 0) {
           n7 = num;
           break;
       }
       else if (place == 8 && n8 == 0) {
           n8 = num;
           break;
       }
       else if (place == 9 && n9 == 0) {
           n9 = num;
           break;
       }
   }
}


void slidingTiles(char direction, int& n1, int& n2, int& n3, int& n4, int& n5, int& n6, int& n7, int& n8, int& n9)
{
   // slide all the tiles with given direction
   if (direction == 'w' || direction == 'W') {
       // can not move first 3 tiles up they are already at top
       // start moving from 4th tile
       // if 2 tiles equals then add number together in one tile
       if (n4 != 0) {
           if (n1 == 0) {
               n1 = n4;
               n4 = 0;
           }
           else if (n1 == n4) {
               n1 = n1 + n4;
               n4 = 0;
           }
       }
       if (n5 != 0) {
           if (n2 == 0) {
               n2 = n5;
               n5 = 0;
           }
           else if (n2 == n5) {
               n2 = n2 + n5;
               n5 = 0;
           }
       }
       if (n6 != 0) {
           if (n3 == 0) {
               n3 = n6;
               n6 = 0;
           }
           else if (n3 == n6) {
               n3 = n3 + n6;
               n6 = 0;
           }
       }
       if (n7 != 0) {
           if (n4 == 0) {
               if (n1 == 0) {
                   n1 = n7;
                   n7 = 0;
               }
               else if (n1 == n7) {
                   n1 = n1 + n7;
                   n7 = 0;
               }
               else {
                   n4 = n7;
                   n7 = 0;
               }
           }
           else if (n4 == n7) {
               n4 = n4 + n7;
               n7 = 0;
           }
       }
       if (n8 != 0) {
           if (n5 == 0) {
               if (n2 == 0) {
                   n2 = n8;
                   n8 = 0;
               }
               else if (n2 == n8) {
                   n2 = n2 + n8;
                   n8 = 0;
               }
               else {
                   n5 = n8;
                   n8 = 0;
               }
           }
           else if (n5 == n8) {
               n5 = n5 + n8;
               n8 = 0;
           }
       }
       if (n9 != 0) {
           if (n6 == 0) {
               if (n3 == 0) {
                   n3 = n9;
                   n9 = 0;
               }
               else if (n3 == n9) {
                   n3 = n3 + n9;
                   n9 = 0;
               }
               else {
                   n6 = n9;
                   n9 = 0;
               }
           }
           else if (n6 == n9) {
               n6 = n6 + n9;
               n9 = 0;
           }
       }
   }
   if (direction == 's' || direction == 'S') {
       // can not move last 3 tiles already at bottom
       if (n4 != 0) {
           if (n7 == 0) {
               n7 = n4;
               n4 = 0;
           }
           else if (n7 == n4) {
               n7 = n7 + n4;
               n4 = 0;
           }
       }
       if (n5 != 0) {
           if (n8 == 0) {
               n8 = n5;
               n5 = 0;
           }
           else if (n8 == n5) {
               n8 = n8 + n5;
               n5 = 0;
           }
       }
       if (n6 != 0) {
           if (n9 == 0) {
               n9 = n6;
               n6 = 0;
           }
           else if (n9 == n6) {
               n9 = n9 + n6;
               n6 = 0;
           }
       }
       if (n1 != 0) {
           if (n4 == 0) {
               if (n7 == 0) {
                   n7 = n1;
                   n1 = 0;
               }
               else if (n1 == n7) {
                   n7 = n7 + n1;
                   n1 = 0;
               }
               else {
                   n4 = n1;
                   n1 = 0;
               }
           }
           else if (n4 == n1) {
               n4 = n4 + n1;
               n1 = 0;
           }
       }
       if (n2 != 0) {
           if (n5 == 0) {
               if (n8 == 0) {
                   n8 = n2;
                   n8 = 0;
               }
               else if (n2 == n8) {
                   n8 = n8 + n2;
                   n2 = 0;
               }
               else {
                   n5 = n2;
                   n2 = 0;
               }
           }
           else if (n5 == n2) {
               n5 = n5 + n2;
               n2 = 0;
           }
       }
       if (n3 != 0) {
           if (n6 == 0) {
               if (n9 == 0) {
                   n9 = n3;
                   n3 = 0;
               }
               else if (n3 == n9) {
                   n9 = n9 + n3;
                   n3 = 0;
               }
               else {
                   n6 = n3;
                   n3 = 0;
               }
           }
           else if (n6 == n3) {
               n6 = n6 + n3;
               n3 = 0;
           }
       }
   }
   if (direction == 'a' || direction == 'A') {
       if (n2 != 0) {
           if (n1 == 0) {
               n1 = n2;
               n2 = 0;
           }
           else if (n1 == n2) {
               n1 = n1 + n2;
               n2 = 0;
           }
       }
       if (n5 != 0) {
           if (n4 == 0) {
               n4 = n5;
               n5 = 0;
           }
           else if (n4 == n5) {
               n4 = n4 + n5;
               n5 = 0;
           }
       }
       if (n8 != 0) {
           if (n7 == 0) {
               n7 = n8;
               n8 = 0;
           }
           else if (n7 == n8) {
               n7 = n7 + n8;
               n8 = 0;
           }
       }
       if (n3 != 0) {
           if (n2 == 0) {
               if (n1 == 0) {
                   n1 = n3;
                   n3 = 0;
               }
               else if (n1 == n3) {
                   n1 = n1 + n3;
                   n3 = 0;
               }
               else {
                   n2 = n3;
                   n3 = 0;
               }
           }
           else if (n2 == n3) {
               n2 = n2 + n3;
               n3 = 0;
           }
       }
       if (n6 != 0) {
           if (n5 == 0) {
               if (n4 == 0) {
                   n4 = n6;
                   n6 = 0;
               }
               else if (n4 == n6) {
                   n4 = n4 + n6;
                   n6 = 0;
               }
               else {
                   n5 = n6;
                   n6 = 0;
               }
           }
           else if (n5 == n6) {
               n5 = n5 + n6;
               n6 = 0;
           }
       }
       if (n9 != 0) {
           if (n8 == 0) {
               if (n7 == 0) {
                   n7 = n9;
                   n9 = 0;
               }
               else if (n7 == n9) {
                   n7 = n7 + n9;
                   n9 = 0;
               }
               else {
                   n8 = n9;
                   n9 = 0;
               }
           }
           else if (n8 == n9) {
               n8 = n8 + n9;
               n9 = 0;
           }
       }
   }
   if (direction == 'd' || direction == 'D') {
       if (n2 != 0) {
           if (n3 == 0) {
               n3 = n2;
               n2 = 0;
           }
           else if (n3 == n2) {
               n3 = n3 + n2;
               n2 = 0;
           }
       }
       if (n5 != 0) {
           if (n6 == 0) {
               n6 = n5;
               n5 = 0;
           }
           else if (n6 == n5) {
               n6 = n6 + n5;
               n5 = 0;
           }
       }
       if (n8 != 0) {
           if (n9 == 0) {
               n9 = n8;
               n8 = 0;
           }
           else if (n9 == n8) {
               n9 = n9 + n8;
               n8 = 0;
           }
       }
       if (n1 != 0) {
           if (n2 == 0) {
               if (n3 == 0) {
                   n3 = n1;
                   n1 = 0;
               }
               else if (n1 == n3) {
                   n3 = n3 + n1;
                   n1 = 0;
               }
               else {
                   n2 = n1;
                   n1 = 0;
               }
           }
           else if (n2 == n1) {
               n2 = n2 + n1;
               n1 = 0;
           }
       }
       if (n4 != 0) {
           if (n5 == 0) {
               if (n6 == 0) {
                   n6 = n4;
                   n4 = 0;
               }
               else if (n4 == n6) {
                   n6 = n6 + n4;
                   n4 = 0;
               }
               else {
                   n5 = n4;
                   n4 = 0;
               }
           }
           else if (n5 == n4) {
               n5 = n5 + n4;
               n4 = 0;
           }
       }
       if (n7 != 0) {
           if (n8 == 0) {
               if (n9 == 0) {
                   n9 = n7;
                   n7 = 0;
               }
               else if (n7 == n9) {
                   n9 = n9 + n7;
                   n7 = 0;
               }
               else {
                   n8 = n7;
                   n7 = 0;
               }
           }
           else if (n8 == n7) {
               n8 = n8 + n7;
               n7 = 0;
           }
       }


   }
}

void print(int& n1, int& n2, int& n3, int& n4, int& n5, int& n6, int& n7, int& n8, int& n9) {
   cout << "---------- " << endl;
   cout << "|" << setw(2) << n1 << "|" << setw(2) << n2 << "|" << setw(2) << n3 << "|" << endl;
   cout << "---------- " << endl;
   cout << "|" << setw(2) << n4 << "|" << setw(2) << n5 << "|" << setw(2) << n6 << "|" << endl;
   cout << "---------- " << endl;
   cout << "|" << setw(2) << n7 << "|" << setw(2) << n8 << "|" << setw(2) << n9 << "|" << endl;
   cout << "---------- " << endl;
}

let me know if you have any problem or doubt. thank you.

Add a comment
Know the answer?
Add Answer to:
C++ I am implementing a simple version of game 2048, which is in 3x3 grid, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLEASE SHOW EACH STEP AND BE VERY CLEAR AND NEAT SO I UNDERSTAND HOW TO SOLVE...

    PLEASE SHOW EACH STEP AND BE VERY CLEAR AND NEAT SO I UNDERSTAND HOW TO SOLVE A GEAR TRAIN, thank you In a simple gear train, use the following information to solve for the 8th shaft speed: Find: n9 Given: Motor Speed: 1750 N1 = 20 N2 = 40 N3 = 64 N4 = 20 N5 = 64 N6 = 30 N7 = 20 N8 =40 N9 = 20 N10 = 64

  • IN JAVA: Write a class, ZeroException, which is an Exception, and is used to signal that somethi...

    IN JAVA: Write a class, ZeroException, which is an Exception, and is used to signal that something is zero when it shouldn't be. Write the class ArrayManipulator which creates an array and provides several methods to manipulate values taken from an array. ZeroException Task: Exceptions are used to signal many types of problems in a program. We can write our own as well to describe specific exceptional conditions which may arise. The advantage of doing so is that when exceptions...

  • I need help in my C++ code regarding outputting the enums in string chars. I created...

    I need help in my C++ code regarding outputting the enums in string chars. I created a switch statement for the enums in order to output words instead of their respective enumerated values, but an error came up regarding a "cout" operator on my "Type of Item" line in my ranged-based for loop near the bottom of the code. I tried casting "i.type" to both "i.GroceryItem::Section::type" and "i.Section::type", but neither worked. Simply put, if a user inputs 1 as their...

  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • Java Assignment Calculator with methods. My code appears below what I need to correct. What I...

    Java Assignment Calculator with methods. My code appears below what I need to correct. What I need to correct is if the user attempts to divide a number by 0, the divide() method is supposed to return Double.NaN, but your divide() method doesn't do this. Instead it does this:     public static double divide(double operand1, double operand2) {         return operand1 / operand2;     } The random method is supposed to return a double within a lower limit and...

  • my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip>...

    my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip> #include<string> #include "bookdata.h" #include "bookinfo.h" #include "invmenu.h" #include "reports.h" #include "cashier.h" using namespace std; const int ARRAYNUM = 20; BookData bookdata[ARRAYNUM]; int main () { bool userChoice = false; while(userChoice == false) { int userInput = 0; bool trueFalse = false; cout << "\n" << setw(45) << "Serendipity Booksellers\n" << setw(39) << "Main Menu\n\n" << "1.Cashier Module\n" << "2.Inventory Database Module\n" << "3.Report Module\n"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT