Got something to write about? Check out our Article Builder.

Beginner C/C++

Moderators: Lundin
Number of threads: 4841
Number of posts: 15800

This Forum Only
Post New Thread

Report
Chess program Posted by powerranger on 16 Nov 2009 at 3:54 AM
Hi all,

I'm trying to do a practice from a book I learn from,
and no success so far.

"This program will be asked to answer whether the black king is threatened – by white Tools (condition called chess), or not threatened.
Threat to the King is that there is a white tool that can access the square which is the black king
during one play.
The program prompts the user to enter three numbers of the row, column, tool, and return on it in a loop
until final input. Input series ends when one of the values of the column, row or tool exceeds
Defined boundaries. I.e.: tool less than - 1, greater than - 5 or coordinates tile extends beyond the boundaries
Board. As in the previous sections, the negative coordinates are considered exception limits the board.
There is no limit on the amount of different types of white tolls on the board, but the black king appears exactly once. (You may assume that the input does contain black king greater than once), given an input for a slot that already taken, error message will appear: "Already taken!"
After the input sequence, the program prints "check!" Or "! No check" in accordance with the state board."

I translate it, so sorry for bad english.
can some help me with an idea? or code?

R.E:
1- King black.
2- white queen.
3 - White Rook.
4 - white knight.
5 - white runner.


I'm a beginner so no functios an such:(

Report
Re: Chess program Posted by AsmGuru62 on 16 Nov 2009 at 5:00 AM
You need a 2D array representing a chess board. Next, you need functions (yes! functions are easy!) to work with the board: put a piece on the cell or check the state of a cell, using its coordinates: row, column.

For example: if you have a KNIGHT at ROW=4 COLUMN=2, then you need to apply the offsets for KNIGHT jump (and you need to do it 8 times, well you should know the steps for a KNIGHT). The same example - one of KNIGHT steps is shift 2 rows up and 1 column to the right - that means you need to ADD these offsets to the KNIGHT location and check the resulting cell for 1) is that cell OFF the board? and 2) is a WHITE KING there? You need to do same checks for each chess piece according to the laws of the game.

The simple model of the board would be a 2D array:

int board [8][8];
int row = 5;
int col = 4;
int cell = board [row][col]; // or is it board[col][row]?.. not sure

Just my opinion: that task is not a beginner task.
Report
Re: Chess program Posted by powerranger on 17 Nov 2009 at 12:01 AM
first of all thnks!
its an assiment that shoud be written without functions/state machine,
this is the diffcukty for me.

I know the Idea, but don't know how to implement it.
I wrote 4 loops, on for each tool, that each one of them have in side more loops that chacking varied of directions, but it's dosn't work.
I will write my program later here..




 
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A bootstrapLabs project.