Results 1 to 2 of 2

Thread: Contest 12: Knight Moves - [2kaud]

  1. #1

    Thread Starter
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Contest 12: Knight Moves - [2kaud]

    C++. The fiddle is at http://ideone.com/n1YBkX

    Code:
    #include <iostream>
    #include <string>
    #include <utility>
    #include <cctype>
    using namespace std;
    
    constexpr int brdsze = 8;	//Size of board
    
    //Validate input and convert to co-ordinates
    //Returns true if OK and false if problem
    bool inpval(const string& inp, pair<int, int>& coord)
    {
    	return (inp.size() == 2) && ((coord.first = toupper(inp[0]) - 'A') >= 0) && (coord.first < brdsze) && ((coord.second = inp[1] - '1') >= 0) && (coord.second < brdsze);
    }
    
    int main()
    {
    	//Offsets for knights possible moves - determines order of display
    	constexpr int kgtpos = 8;
    	constexpr pair<int, int> kgtmov[kgtpos] = { {-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, -1}, {2, 1} };
    
    	string inp;
    	pair<int, int> pos;
    
    	//Get and validate starting position
    	do {
    		cout << "Enter starting position: ";
    		getline(cin, inp);
    	} while (!inpval(inp, pos) && (cout << "\nInvalid starting position.\nInput format is CR where\nC is A to " << (char)('A' + brdsze - 1) << "\nR is 1 to " << brdsze << endl));
    
    	//Find last valid position
    	int last = kgtpos - 1;
    
    	for (int got = false, rw, cl; !got; --last)
    		got = ((cl = pos.first + kgtmov[last].first) >= 0) && (cl < brdsze) && ((rw = pos.second + kgtmov[last].second) >= 0) && (rw < brdsze);
    
    	cout << "\nAvailable positions from " << inp << " are" << endl;
    
    	//Find positions from first up to found last position and output in required format
    	for (int pst = 0, first = 0, lst = last + 1, rw, cl; pst <= lst; ++pst)
    		if (((cl = pos.first + kgtmov[pst].first) >= 0) && (cl < brdsze) && ((rw = pos.second + kgtmov[pst].second) >= 0) && (rw < brdsze))
    			cout << (first++ ? ((pst == lst) ? ", and " : ", ") : "") << char(cl + 'A') << rw + 1;
    
    	cout << endl;
    }
    This uses one pass of the array kgtmov[] (split into 2 parts) so that the output is in the required format.

    Sample output
    Code:
    Enter starting position: g1
    
    Available positions from g1 are
    E2, F3, and H3
    
    Enter starting position: c6
    
    Available positions from c6 are
    A5, A7, B4, B8, D4, D8, E5, and E7
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Contest 12: Knight Moves - [2kaud]

    I've had a third-party who is not active on VBForums judge this particular entry and these were his notes:

    Accuracy - Does the program follow all of the rules? If you mean the chess rules, yes it shows only legal moves.
    Length - How concise is the submitted code? It's very concise, actually too concise for my taste. Less concise would make it easier to read and understand.
    Execution - How quick did the code execute? The reply is instantaneous.
    Efficiency - How efficient is the algorithm(s) you chose, could it have been optimized? I don't think so. The program checks only the maximum of 8 moves.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width