How to make user type in coordinates for a point

Im very new to c++ and i need help with this, would be much appreciated. what im trying to do is make a game called Malom/ Nine Mens Morris where you display the board... where you use varabiles to store locations of the points and users type in location of where the points are placed. My board got messed up when i copy and pasted it but i just need help with the board so a user can type in coords (1,A) and an X shows up if some can some1 show me what to do for 1 point id be able to finish this thanks in advance.... what my chart is sopost to look like

http://www.asij.ac.jp/elementary/gr1web/images/Ninemen.jpg

cout << " 1 |-------------|-------------| " << endl;

cout << " | | | " << endl;

cout << " 2 | |---------|---------| | " << endl;

cout << " | | | | | " << endl;

cout << " 3 | | |-----|-----| | | " << endl;

cout << " | | | | | | " << endl;

cout << " | | | | | | " << endl;

cout << " | | | | | | " << endl;

cout << " 4 |---|---| |---|---| " << endl;

cout << " | | | | | | " << endl;

cout << " | | | | | | " << endl;

cout << " | | | | | | " << endl;

cout << " 5 | | |-----|-----| | | " << endl;

cout << " | | | | | " << endl;

cout << " 6 | |---------|---------| | " << endl;

cout << " | | | " << endl;

cout << " 7 |-------------|-------------| " << endl;

cout << " A B C D E F G " << endl;
Last edited on
I'm not sure I understand your problem correctly, but you can try asking user to input two chars and then subtracting 49 from the first one and 65 from the second one to convert them from ascii chars to numbers. That way you can get the coordinates of a point.
To draw 'X' you can put whole board into an array befote drawing it.
Just ask for the user to input a coordinate and then parse it yourself:
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
#include <cctype>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

struct coord_t
  {
  int row;
  int col;
  coord_t( int row = 0, int col = 0 ): row( row ), col( col ) { }
  bool valid() const;
  };

coord_t ask_coordinate()
  {
  coord_t           result;
  string            user_input;
  string::size_type index;

  cout << "Please enter a valid coordinate pair> " << flush;
  while (true)
    {
    getline( cin, user_input );

    try
      {
      index = user_input.find_first_of( "1234567" );
      if (index == string::npos) throw 0;

      istringstream rowss( user_input.substr( index ) );
      rowss >> result.row;
      if (!rowss) throw 0;
      --result.row;  // shift it to 0..6

      index = user_input.find_first_of( "ABCDEFGabcdefg" );
      if (index == string::npos) throw 0;

      result.col = toupper( user_input[ index ] ) -'A';  // range 0..6

      if (!result.valid()) throw 1;

      break;
      }
    catch (int n)
      {
      if (n == 0)
        cout << "Please enter a pair like \"7A\" or \"A-7\" or \"(A,7)\" etc> " << flush; 
      else
        cout << "Invalid pair. Try again> " << flush;
      }
    } // Yoinks! Thanks Hammurabi!

  return result;
  }

bool coord_t::valid() const
  {
  int r = row -3;
  if (r < 0) r = -r;
  return ((col >= 0) && (col <= 6))
    && (
         ((r == 3) && !(col % 3))
      || ((r == 2) &&  (col % 2))
      || ((r == 1) &&  (col > 1) && (col < 5))
      || ((r == 0) &&  (col != 0))
       );
  }
Untested code!

Hope this helps.
Last edited on
cout << " 1 |-------------|-------------| " << endl;
cout << " | | | " << endl;
cout << " 2 | |---------|---------| | " << endl;
cout << " | | | | | " << endl;
cout << " 3 | | |-----|-----| | | " << endl;
cout << " | | | | | | " << endl;
cout << " | | | | | | " << endl;
cout << " | | | | | | " << endl;
cout << " 4 |---|---| |---|---| " << endl;
cout << " | | | | | | " << endl;
cout << " | | | | | | " << endl;
cout << " | | | | | | " << endl;
cout << " 5 | | |-----|-----| | | " << endl;
cout << " | | | | | " << endl;
cout << " 6 | |---------|---------| | " << endl;
cout << " | | | " << endl;
cout << " 7 |-------------|-------------| " << endl;
cout << " A B C D E F G " << endl;
Ah, I think I misunderstood your original question. (In part because you are not using [code] tags.)

When you are outputting stuff, just place the appropriate piece in each given location:
1
2
3
4
5
6
7
8
void print_gameboard( char gameboard[ 7 ][ 7 ] )
  {
  cout << "1  " << gameboard[ 0 ][ 0 ]
       <<     "-------------" << gameboard[ 0 ][ 3 ]
       <<                   "-------------" << gameboard[ 0 ][ 6 ] << '\n';
  cout << "   |             |             |\n";
  ...
  }

Hope this helps.
Topic archived. No new replies allowed.