Going over the board + layout issue

Hi there, I am wondering what I am doing wrong ss when a user inputs anything on the 10th row..the 'X' char is misplaced :s

Also, I cannot seem to limit the user from inputting BEYOND the board..I feel like I've taken a wrong step somewhere.

Thanks once again

Also, I do know that I could use structs and enums to make things easier for myself, but I have no idea on how they work.
I know the synatax but do not know how to implement them to my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <cstdio>
    #include <vector>
    #include <fstream>
    #include <ctime>
     
    using namespace std;
    char matrix[10][10]; //Sets up grid for Ships
    int m,n,x,y,a,i,j;
    void welcomeScreen();
    void board();
     
    const int aircraftCarrier=5;
    const int battleship=4;
    const int destroyer=3;
    const int submarine=3;
    const int boat=2;
     
    #define interLine " || ---|---|---|---|---|---|---|---|---|---|\n"
     
     
    void checkShip()
    {
    int k,size=5;
    int direction;
    for(int m=0;m<5;m++){
    cout<<"Enter X value for your Aircraft Carrier"<<endl;
    cin>>x; --x;
    cout<<"Enter Y value for your Aircraft Carrier"<<endl;
    cin>>y; --y;
    cout<<"Enter Direction, 1[up], 2[down] : "<<endl;
    cin>>direction;
    cout<<endl<<endl;
    switch(direction){
    case 1:
    if((x-size+1)<0)
    {
    cout<<"Error!!!"<<endl;
    }
    else
    {
    for (k=0;k<5;k++)
    {
    matrix[x-k][y]='x';
    }
    }
    break;
     
    case 2:
    if((x-size+1)<0)
    {
    cout<<"Nuh uh!"<<endl;
    }
    else
    {
    for (k=0;k<5;k++)
    {
    matrix[x+k][y]='x';
    }
    }
    break;
    case 3:
    if((y-size+1)<0)
    {
    cout<<"Error!"<<endl;
    }
    else
    {
    for (k=0;k<5;k++)
    {
    matrix[x][y-k+1]='x';
    }
    }
    break;
    case 4:
    if((y-size-1)<0)
    {
    cout<<"Say WHAA?"<<endl;
    }
    else
    {
    for (k=0;k<5;k++)
    {
    matrix[x][y-k-1]='x';
    }
    }
    break;
    }
    }
    }
     
    int main()
    {
    srand(time(NULL));
    welcomeScreen();
     
    for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
    matrix[i][j] = ' ';
    }
    }
    board();
    checkShip();
    system("CLS");
    board();
    cin.get();
    return 0;
    }
     
    void randomShip()
    {
    srand(time(NULL));
     
    }
    void welcomeScreen()//Welcome Screen
    {
    int choice;
    cout<<"\t Hi there, Welcome to Battleships. The game works as follows,"<<endl;
    cout<<"\t In this game each player places a fleet of several ships onto a"<<endl;
    cout<<"\t 2D grid in our case you will decide if you want to place your "<<endl;
    cout<<"\t ships manuallyor if you wish to set it randomly."<<endl;
    cout<<"\t The computer will always set their fleet randomly."<<endl;
    cout<<"\t Please enter your choice now:"<<endl;
    cout<<"\n\n\n\n";
    cout<<"\t 1. Set ships manually."<<endl;
    cout<<"\t 2. Set ships randomly."<<endl;
    cout<<"\t 3. Exit Game."<<endl;
    cout<<endl;
    cin>>choice;
    switch(choice)
    {
    case 1:
    void checkShip();
    break;
    case 2:
    void randomShip();
    break;
    case 3:
    cout<<"Thanks for playing, Goodbye :)"<<endl;
    exit(0);
    break;
    default:
    cout<<"You've chosen the wrong option, please try again \n";
    exit(0);
    break;
    }
     
    }
     
    void board()//Sets up Board
    {
    cout<<" B \n"<<endl;
    cout<<" --------------------------------------------"<<endl;
    cout<<" --------------------------------------------\n"<<endl;
    cout<<"\n\t 1 2 3 4 5 6 7 8 9 10\n"<<endl;
     
    // 10 horizontal rows
    for(int i=0; i < 10;i++)
    {
    cout << " || " <<(i + 1);
    // 10 vertical rows
    for(int j=0; j < 10;j++) {
    cout << " " << matrix[i][j] << " ";
    }
    cout << endl << interLine;
    }
    cout << "\n\n\n";
    }
Topic archived. No new replies allowed.