Java board game

前端 未结 1 1024
慢半拍i
慢半拍i 2021-01-15 08:25

Basically I\'m creating a board game in java and have managed to create cells, using arrays, to look like a 10x10 grid. Now I\'ve numbered them they go from left to right t

1条回答
  •  孤街浪徒
    2021-01-15 09:00

    Ok so I won't write the exact code for you, but I'll show you an example of how to do it with a regular 2D array. I only have a C++ compiler available right now, but it should be clear enough:

    So basically you need to cycle through rows from end to start. That's why the first, outer loop you're going from 9 to 0. This will start at bottom row and finish at the top row, thus reversing.

    for (int i = 9; i >= 0; i--) {
    
        // now the trick to making a "zig-zag" is to alternate between two ways
        // of printing out each row. if i is even, you print out from right to left
    
        if (i % 2)
            for (int j = 9; j >= 0; j--)
                cout << numbers[i][j] << "\t";
    
        // and if i is odd, you print it out from left to right
        else
            for (int j = 0; j < 10; j++)
                cout << numbers[i][j] << "\t";
    
        cout << endl;
    
    }
    

    Result: enter image description here

    0 讨论(0)
提交回复
热议问题