ABXZone Computer  Forums



Reply
 
Thread Tools Display Modes
Old 04-04-2009, 10:38 AM   #1
Tbolt
Registered User
 
Tbolt's Avatar
 
Join Date: Jul 2006
Posts: 5
Nested looping in C - Im stuck

So I thought I had a pretty good understanding of looping even though I missed class the day it was taught. However now I am in a conceptual mind**** of how to approach this.

The assignment is to create a dynamic tic-tac-toe board using multidimensional arrays and functions (with array passing)

here is what the output should look like:
hxxp://i4.photobucket.com/albums/y108/phishkids11/toe1.jpg


here is what I have so far, focusing on the display board part
Code:
// INCLUDES
#include <stdio.h>

// DEFINES
#ifndef __TRUE_FALSE__
#define __TRUE_FALSE__
#define TRUE 1
#define FALSE 0
#endif

// ROWS and COLS
#define ROWS 7
#define COLS 7

#define MARKONE 'X'
#define MARKTWO 'O'
#define BLANK   ' '

// PROTOTYPES
void InitializeBoard(char[ROWS][COLS]);
void DisplayBoard(char[ROWS][COLS]);
int PlayerMove(int, int, char[ROWS][COLS], char);


// MAIN
int main() {
	// declare variables
	char board[ROWS][COLS];

	// initialize board
	InitializeBoard(board);
/*
	// populate the board with moves
	PlayerMove(1, 1, board, MARKONE);
	PlayerMove(1, 2, board, MARKONE);
	PlayerMove(4, 3, board, MARKONE);
	PlayerMove(1, 1, board, MARKTWO);
	PlayerMove(6, 2, board, MARKTWO);
	PlayerMove(4, 12, board, MARKTWO);
*/
	// display the board
	DisplayBoard(board);
	
	// exit program
	return 0;
}

// FUNCTION IMPLEMENTATIONS
void InitializeBoard(char board[ROWS][COLS]) {
	int i = 0, j = 0;
	for(i = 0; i < ROWS; i++) {
		for(j = 0; j < COLS; j++) {
			board[i][j] = BLANK;
		}
	}
}

void DisplayBoard(char board[ROWS][COLS]) {
	int i = 0, j = 0, k = 0;
	printf("**************TIC-TAC-TOE**************\n\n");

	for(i = 1; i <= COLS; i++) {
		printf("%2d", i);
	}
		printf("\n\n");    
		
        

}
			



/*
int PlayerMove(int row, int col, char board[ROWS][COLS], char symbol) {
	
}
*/
I feel like I am trying to approach this from the wrong angle, and because of that I can't comprehend how to do it. I imagine I will need to nest a loop within a loop within another loop.

Any advice would be great, not necessarily with this particular problem but anything with looping that would help me understand it better would be great.
(Offline)   Reply With Quote

Advertisement [Remove Advertisement]
Old 04-04-2009, 04:41 PM   #2
XJ.
Registered User
 
XJ.'s Avatar
 
Join Date: Apr 2002
Location: USA-NH
Posts: 8,429
Re: Nested looping in C - Im stuck

For your DisplayBoard() function, just do it the same way you did the InitializeBoard() function. Just replace 'board[i][j] = BLANK' with the appropriate print function.

Good luck!
__________________
Asus P6T V2 Deluxe Ci7 970 @ 4.0GHz w/HT, Corsair H100i, 2x240GB SanDisk Extreme RAID0, 1x WD VR 150GB,
MSI GTX 680 PE @ 1110MHz, 12GB G.Skill Ripjaws DDR3 1600, Corair 850HX, Corsair 800D case. Win10 Pro x64. Dell U2412M.

(Offline)   Reply With Quote
Old 04-04-2009, 06:48 PM   #3
Tbolt
Registered User
 
Tbolt's Avatar
 
Join Date: Jul 2006
Posts: 5
Re: Nested looping in C - Im stuck

Alright, I am starting to get it. I just had to go back to the basics and play around a bit before tackling this.

Thanks though =p
(Offline)   Reply With Quote
Old 04-04-2009, 09:03 PM   #4
zapionics<alt>
&lt;unknown level&gt;
 
zapionics<alt>'s Avatar
 
Join Date: Dec 2002
Location: AUSTRALIA
Posts: 6,105
Re: Nested looping in C - Im stuck

just on this "developers block" syndrome you seemed to be experiencing... I'm a big fan of design... ie: conceptualising the solution via a process of design which then gives you a skeletal structure in which to hang your code.

I tend to use "structure" charts for this... and use cases of course... and activity diagrams or even sequence diagrams...

I know this may be a trivial example for some ppl... coding top down has advantages (eg: speed) if you are very familiar and experienced with the solution domain... but otherwise... design!
__________________
***
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Please note: any advertising within this post has been placed there by the site owner and NOT by me!
Why not avoid the ads by joining the 'Zone today? The site is mostly ad free for members!
***



"You Cannot Hide..."
(Offline)   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com

© 2006 - 2016 ABXZone Forums | About ABX Zone Forums | Advertisers | Investors | Legal | A member of the Crowdgather Forum Community