I am writing a TicTacToe game in C++. I am using a 2D character array to input each player's piece into a square. However, for input I am asking the user to type in A1, which will be board[0][0] and the top left corner of the game board. How do I allow the user to type in A1 but still allow that to be board[0][0] without hard coding? Also, the user can determine the board size. Min: 3x3, max: 13x16.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- 放在input的text下文本一直出现一个/(即使还没输入任何值)是什么情况
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Sum multidimensional array C#
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
(Assuming the board is 3x3, or at most 9x9)
Get two characters from the user, ensure the first is an uppercase letter and the second is a digit, and that they're in the right range.
Note that - on most platforms you are likely to be working on - if the character variable
c
has value'A'
,'B'
or'C'
then the expresionc - 'A'
has value0
,1
or2
, which you can use as an index. It's not 100% portable code, though, as @SomeProgrammerDude notes.... if the board is larger you may have to allow for multiple-character specification of column and row, and parse them appropriately. The
c - 'A'
indexing will work all the way up to'Z'
of course.