Simulate a cube that can be rotated in C ++ (1)

Source: Internet
Author: User

I have been playing the cube, and I have always wanted to program it.

 

I finally started today ~

 

Ideas:

1. divide the cube into six slices (each cube is a cube of 1 × 3 × 3) and use a 5 × 5 char array, W, Y, B, G, respectively, o, r fill array with six letters to represent various colors

Example: [] [B] [B] [B] [] B-BLUE O-orange w-White R-Red G-green

[O] [W] [W] [W] [R]

[O] [W] [W] [W] [R]

[O] [W] [W] [W] [R]

[] [G] [g] [g] []

 

(The so-called "1*3*3" is the area marked by the lawn light)

2. Create a piece class and add the rotation attributes of the slices: 90 degrees clockwise, 90 degrees clockwise, and 180 degrees Celsius.

 

3. Create a cube class. There are 6 pieces in the class that correspond to 6 of the cube,

Create Function 1 to Control the effect of each slice rotation on other slice.

Create Function 2 and read user commands to control cube rotation (such as U, L, R)

 

4. Call the C ++ graphics library to visualize the cube class. (There is no contact with this part)

 

 

 

Code of the slice:

 // Piece. h-cube Category </P> <p> # include <iostream. h> <br/> # define M 5 <br/> // ======================== ===============================< br/> class piece <br/> {<br/> PRIVATE: <br/> char a [m] [m]; <br/> Public: <br/> piece (char X [m] [m]) // constructor introduces the array in the main program for initialization <br/>{< br/> for (INT I = 0; I <m; I ++) <br/> for (Int J = 0; j <m; j ++) <br/> A [I] [J] = x [I] [J]; <br/>}</P> <p> void cirni (void) // counter-clockwise rotation function <br/>{< br/> int B [m] [m]; <br/> int I, j; <br/> for (I = 0; I <m; I ++) <br/> for (j = 0; j <m; j ++) <br/> B [M-1-j] [I] = A [I] [J]; // convert array a to array B <br/> for (I = 0; I <m; I ++) <br/> for (j = 0; j <m; j ++) <br/> A [I] [J] = B [I] [J]; <br/>}</P> <p> void cirsh (void) // clockwise rotation function <br/>{< br/> int B [m] [m]; <br/> int I, j; <br/> for (I = 0; I <m; I ++) <br/> for (j = 0; j <m; j ++) <br/> B [J] [M-i-1] = A [I] [J]; <br/> for (I = 0; I <m; I ++) <br/> for (j = 0; j <m; j ++) <br/> A [I] [J] = B [I] [J]; <br/>}</P> <p> void cirnipp (void) // 180 degree rotation function <br/>{< br/> int B [m] [m]; <br/> int I, J; <br/> for (I = 0; I <m; I ++) <br/> for (j = 0; j <m; j ++) <br/> B [M-i-1] [M-j-1] = A [I] [J]; <br/> for (I = 0; I <m; I ++) <br/> for (j = 0; j <m; j ++) <br/> A [I] [J] = B [I] [J]; <br/>}</P> <p> void show (void) // print the array <br/> {<br/> for (INT I = 0; I <m; I ++) <br/> {<br/> for (Int J = 0; j <m; j ++) cout <A [I] [J] <""; <br/> cout <Endl; <br/>}</P> <p> };

 

The idea of this header file comes from here: http://zhidao.baidu.com/question/165615023.html? SI = 1

 

Core Statement:

B [M-1-j] [I] = A [I] [J]; clockwise rotation

B [J] [M-i-1] = A [I] [J]; clockwise rotation

B [M-i-1] [M-j-1] = A [I] [J]; 180 degree rotation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.