Entwickler-Ecke
Multimedia / Grafik - [Anfänger Frage] Simples Spiel mit Instanz Problem
neon - Sa 04.10.03 02:32
Titel: [Anfänger Frage] Simples Spiel mit Instanz Problem
Hallo,
ich habe ein kleines Spiel:
Es gibt SpielerA und SpielerB;
SpielerA hat 100 Punkte und SpielerB 120;
SpielerA gibt bei jedem Zug 0.16 seiner Steine an SpielerB ab.
SpielerB gibt bei jedem Zug 0.18 seiner Steine an SpielerA ab.
ist recht simpel.
Dafür habe ich eine klasse geschrieben (PlayerClass) und sie unter player.pas gespeichert.
Dann habe ich eine weitere klasse in der datei playergui.pas in der das TForm erzeugt wird.
Unter "uses" ist die player datei angegeben, damit ich in der playergui.pas die objekte und instanzen erzeugen kann(von PlayerClass).
Eine prodzedur in der PlayerClass ist, das bei ihrem aufruft ein wert ermittelt wird, und dann dem anderen Spieler mitgeteilt werden soll.
und nun das problem: wenn PlayerA diese prozedur aufruft und dann an PlayerB übergeben will, und die instanzen heissen PlayerA und PlayerB ( erzeugt in playergui.pas; Klasse liegt in player.pas), dann kann ich in der klasse von playerA aus nicht auf PlayerB zugreifen?! :(
Kann mir da vielleicht einer weiterhelfen?
Hier ist der quellcode, die Stelle an der PlayerA(oder B) an PlayerB(oder A) einen Wert übergeben soll, habe ich mit einem Kommentar versehen:
player.pas
Quelltext
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:
| unit player;
interface uses SysUtils, Classes;
type PlayerClass = class(TObject) constructor Create;
private NumPercent, NumDelivery: double; public NumCount: double; procedure CalculateDelivery; procedure CalculateAcceptance(NewValue: double); end;
implementation constructor PlayerClass.Create; begin //null end;
procedure PlayerClass.CalculateDelivery; begin NumDelivery:= NumPercent*NumCount; NumCount:= NumCount-NumDelivery;
// PlayerA(oder B) abgabe von NumCount and PlayerB(oder A)
end;
procedure PlayerClass.CalculateAcceptance(NewValue: double); begin NumCount:= NumCount+NewValue; end;
end. |
playergui.pas
Quelltext
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:
| unit PlayerGui;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, player;
type TForm1 = class(TForm) PlayerNameA_Title2: TLabel; Title1: TLabel; Title2: TLabel; PlayerNameB_Title2: TLabel; Title3: TLabel; PlayerNameA_Title3: TLabel; PlayerNameB_Title3: TLabel; Title4: TLabel; PlayerNameA_Title4: TLabel; PlayerNameB_Title4: TLabel; PlayeAValue_Title2: TEdit; PlayeBValue_Title2: TEdit; PlayeAValue_Title3: TEdit; PlayeBValue_Title3: TEdit; ButtonCalculate: TButton; PlayeAResult_Title4: TEdit; PlayeBResult_Title4: TEdit;
procedure ButtonCalculateClick(Sender: TObject); procedure FormCreate(Sender: TObject);
private { Private-Deklarationen } public end;
var Form1: TForm1; var PlayerA, PlayerB: player.PlayerClass;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject); begin PlayerA:= PlayerClass.Create; PlayerB:= PlayerClass.Create; end;
procedure TForm1.ButtonCalculateClick(Sender: TObject); begin PlayerA.CalculateDelivery; end;
end. |
playergui.dfm
Quelltext
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: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236:
| object Form1: TForm1 Left = 689 Top = 208 Width = 330 Height = 340 Caption = 'PlayerGUI' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 13 object PlayerNameA_Title2: TLabel Left = 12 Top = 92 Width = 58 Height = 16 Caption = 'Spieler A:' Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -13 Font.Name = 'MS Sans Serif' Font.Style = [] ParentFont = False end object Title1: TLabel Left = 61 Top = 16 Width = 187 Height = 24 Caption = 'Simples Rechen-Spiel' Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -20 Font.Name = 'MS Sans Serif' Font.Style = [] ParentFont = False end object Title2: TLabel Left = 7 Top = 70 Width = 79 Height = 16 Caption = 'Grundwerte' Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -15 Font.Name = 'MS Sans Serif' Font.Style = [fsBold] ParentFont = False end object PlayerNameB_Title2: TLabel Left = 172 Top = 92 Width = 58 Height = 16 Caption = 'Spieler B:' Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -13 Font.Name = 'MS Sans Serif' Font.Style = [] ParentFont = False end object Title3: TLabel Left = 7 Top = 125 Width = 64 Height = 16 Caption = 'Abgaben' Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -15 Font.Name = 'MS Sans Serif' Font.Style = [fsBold] ParentFont = False end object PlayerNameA_Title3: TLabel Left = 12 Top = 147 Width = 58 Height = 16 Caption = 'Spieler A:' Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -13 Font.Name = 'MS Sans Serif' Font.Style = [] ParentFont = False end object PlayerNameB_Title3: TLabel Left = 171 Top = 147 Width = 58 Height = 16 Caption = 'Spieler B:' Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -13 Font.Name = 'MS Sans Serif' Font.Style = [] ParentFont = False end object Title4: TLabel Left = 7 Top = 236 Width = 80 Height = 16 Caption = 'Ergebnisse' Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -15 Font.Name = 'MS Sans Serif' Font.Style = [fsBold] ParentFont = False end object PlayerNameA_Title4: TLabel Left = 11 Top = 260 Width = 58 Height = 16 Caption = 'Spieler A:' Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -13 Font.Name = 'MS Sans Serif' Font.Style = [] ParentFont = False end object PlayerNameB_Title4: TLabel Left = 171 Top = 259 Width = 58 Height = 16 Caption = 'Spieler B:' Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -13 Font.Name = 'MS Sans Serif' Font.Style = [] ParentFont = False end object PlayeAValue_Title2: TEdit Left = 80 Top = 90 Width = 65 Height = 21 Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] ParentFont = False TabOrder = 0 Text = '100' end object PlayeBValue_Title2: TEdit Left = 239 Top = 90 Width = 65 Height = 21 Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] ParentFont = False TabOrder = 1 Text = '100' end object PlayeAValue_Title3: TEdit Left = 80 Top = 145 Width = 65 Height = 21 Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] ParentFont = False TabOrder = 2 end object PlayeBValue_Title3: TEdit Left = 239 Top = 145 Width = 65 Height = 21 Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] ParentFont = False TabOrder = 3 end object ButtonCalculate: TButton Left = 8 Top = 184 Width = 65 Height = 25 Caption = 'Berechnen' TabOrder = 4 OnClick = ButtonCalculateClick end object PlayeAResult_Title4: TEdit Left = 79 Top = 258 Width = 65 Height = 21 Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] ParentFont = False TabOrder = 5 end object PlayeBResult_Title4: TEdit Left = 238 Top = 258 Width = 65 Height = 21 Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] ParentFont = False TabOrder = 6 end end |
ps: ich bin anfänger *G*
pps: ich weiss, die klasse ist noch nicht fertig geschriebe an manchen stelen...
mfg
neon
Anonymous - Sa 04.10.03 08:38
Herzlich Willkommen im DF!
zu deinem Problem:
ändere die Deklaration so um:
Delphi-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
| type PPlayerClass = ^TPlayerClass; TPlayerClass = class(TObject) constructor Create; private NumPercent, NumDelivery: double; public NumCount: double; p2: PPlayerClass; procedure CalculateDelivery; procedure CalculateAcceptance(NewValue: double); end; |
den setzt du dann so:
Delphi-Quelltext
1:
| PlayerA.p2 := @PlayerB; |
und so rufst Methoden auf:
Delphi-Quelltext
1:
| p2^.CalculateDelivery; |
neon - Sa 04.10.03 12:14
Hallo obbschtkuche,
erstmal danke für das freundliche Hallo :)
dann folgendes:
Also, so ganz habe ich das was du geschrieben hast nicht verstanden!
Ich deklariere ja, die beiden objekte PlayerA und PlayerB in playergui.pas:
Delphi-Quelltext
1: 2:
| var PlayerA, PlayerB: player.PlayerClass; |
und beim erstellen der Form dann so:
Delphi-Quelltext
1: 2: 3: 4: 5:
| procedure TForm1.FormCreate(Sender: TObject); begin PlayerA:= PlayerClass.Create; PlayerB:= PlayerClass.Create; end; |
ist das nun überflüssig, oder muss das trotzdem da stehen bleiben?!
weil ich nicht ganz verstehe wo ich
Delphi-Quelltext
1:
| PlayerA.p2 := @PlayerB; |
deklaieren muss und soll?!
schon mal danke für deine mühe :)
ps: noch eine frage:
ich will ja auch werte von der player.pas (PlayerClass) aus, bei der TForm1 auslesen, aus Labels oder Edit feldern... wie mache ich das dann?
mfg
neon
Anonymous - Sa 04.10.03 12:43
das was ich vorher geschrieben hatte, war der weg über Pointer. So sollte es aber auch gehen:
Delphi-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
| unit Player;
uses ...;
implementation
uses Unit1;
unit1.PlayerA.CalculateDelivery;
end. |
dann ist die Unit aber auch nur in diesem einen Projekt verwendbar. Aber ich vermute dass das in diesem Fall egal ist...[/delphi]
neon - Sa 04.10.03 15:02
Hallo,
vielen Dank!
das hat super geklappt :)
vielleicht wäre jemand gerade noch im stande, bei folgendem Hilfe zu leistem:
Ich inizialisiere eine variable:
in einer klasse,
aber ich addiere etwas bevor, ich ihr einen festen wert zuweisen kann,...
also würde ich die variablen gerne vorher auf 0; setzen, allerdings hilfet das nichts, weil wenn ich dann etwas addiere ist die variablen trotzdem leer?! :((
und: wie kann ich überprüfen ob eine variable einen inhalt hat?
mfg
neon
Anonymous - Sa 04.10.03 15:09
wenn es ein Pointer ist kannst du mit p <> nil überprüfen ob er auf etwas zeigt.
Bei Variablen geht das nicht.
| Zitat: |
aber ich addiere etwas bevor, ich ihr einen festen wert zuweisen kann,...
also würde ich die variablen gerne vorher auf 0; setzen, allerdings hilfet das nichts, weil wenn ich dann etwas addiere ist die variablen trotzdem leer?! ( |
das verstehe ich nicht wirklich. Poste mal Sourcecode.[/delphi]
neon - Sa 04.10.03 15:32
Hallo,
also hier der code:
player.pas:
Delphi-Quelltext
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: 69: 70: 71: 72: 73: 74: 75:
| unit player;
interface uses SysUtils, Classes;
type PlayerClass = class(TObject) constructor Create;
private NumDelivery: double; public NumCount, NumPercent, NumResult: double; procedure CalculateDelivery(PlayerX: string); procedure CalculateAcceptance(NumDelivery: double); end;
implementation uses PlayerGui;
constructor PlayerClass.Create; begin end;
procedure PlayerClass.CalculateDelivery(PlayerX: string); begin if PlayerX = 'A' then begin if PlayerGui.Form1.PlayerAResult_Title4.Text = '' then begin NumCount:= StrToFloat(PlayerGui.Form1.PlayerAValue_Title2.Text); PlayerGui.PlayerB.NumDelivery:= 100; end else NumCount:= StrToFloat(PlayerGui.Form1.PlayerAResult_Title4.Text);
NumPercent:= StrToFloat(PlayerGui.Form1.PlayerAValue_Title3.Text);
NumDelivery:= NumCount*NumPercent; NumCount:= NumCount-NumDelivery;
PlayerGui.PlayerB.CalculateAcceptance(NumDelivery); end else if PlayerX = 'B' then begin if PlayerGui.Form1.PlayerBResult_Title4.Text = '' then NumCount:= StrToFloat(PlayerGui.Form1.PlayerBValue_Title2.Text) else NumCount:= StrToFloat(PlayerGui.Form1.PlayerBResult_Title4.Text);
NumPercent:= StrToFloat(PlayerGui.Form1.PlayerBValue_Title3.Text);
NumDelivery:= NumCount*NumPercent; NumCount:= NumCount-NumDelivery;
PlayerGui.PlayerA.CalculateAcceptance(NumDelivery); end end;
procedure PlayerClass.CalculateAcceptance(NumDelivery: double); begin NumCount:= NumCount+NumDelivery; end; end. |
playergui.pas:
Delphi-Quelltext
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:
| unit PlayerGui;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, player;
type TForm1 = class(TForm)
PlayerNameA_Title2: TLabel; Title1: TLabel; Title2: TLabel; PlayerNameB_Title2: TLabel; Title3: TLabel; PlayerNameA_Title3: TLabel; PlayerNameB_Title3: TLabel; Title4: TLabel; PlayerNameA_Title4: TLabel; PlayerNameB_Title4: TLabel; PlayerAValue_Title2: TEdit; PlayerBValue_Title2: TEdit; PlayerAValue_Title3: TEdit; PlayerBValue_Title3: TEdit; ButtonCalculate: TButton; PlayerAResult_Title4: TEdit; PlayerBResult_Title4: TEdit;
procedure ButtonCalculateClick(Sender: TObject); procedure FormCreate(Sender: TObject);
private public end;
var Form1: TForm1; var PlayerA, PlayerB: player.PlayerClass;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject); begin PlayerA:= PlayerClass.Create; PlayerB:= PlayerClass.Create; end;
procedure TForm1.ButtonCalculateClick(Sender: TObject); begin PlayerA.CalculateDelivery('A'); PlayerB.CalculateDelivery('B');
Form1.PlayerAResult_Title4.Text:= FloatToStr(PlayerA.NumCount); Form1.PlayerBResult_Title4.Text:= FloatToStr(PlayerB.NumCount); end;
end. |
so, die rechnung wäre ja nun:
(bei Abgaben: PlayerA: 0,16 - PlayerB: 0,17)
PlayerA: Nimmt 100 aus dem TextFeld rechnet ihn mal 0,16;
100*0,16 = 16;
das ist der anteil den er abgibt an PlayerB:
100-16 = 84; // für sich selber, weil er die ja abgibt.
dann ruft er eine funktion auf, um diese 16 an den anderen spieler zu übermitteln damit er sie addieren kann...
das selbe macht dannach playerB,
die ergebnisse müssten also lauten:
PlayerA: 101 (100-16(100*0,16); 84+17(100*0,17) = 101)
PlayerB: 99 (100-17(100*0,17); 83+16(100*0,16) = 99)
aber die, die er ausrechnet sind:
PlayerA: 101;
PlayerB: 83;
und weil der wer 83 bei PlayerB ist, er aber 99 sein müsste, und das ergebnis von 100-17 genau 83 entspricht, denke ich addiert er die abgabe von PlayerA (16) nicht dazu.
Die Abgabe von PlayerA (16) an PlayerB erfolgt aber bevor, das Programm auf 83 kommen kann um dann mit 16 auf 99 zu kommen..
naja ich hoffe das hast du verstanden ;)
//EDIT:
yuhu, habe den Fehler selber gefunden!! - Danke für deine Hilfe obbschtkuche!!!!!!! :)
mfg
neon
Entwickler-Ecke.de based on phpBB
Copyright 2002 - 2011 by Tino Teuber, Copyright 2011 - 2026 by Christian Stelzmann Alle Rechte vorbehalten.
Alle Beiträge stammen von dritten Personen und dürfen geltendes Recht nicht verletzen.
Entwickler-Ecke und die zugehörigen Webseiten distanzieren sich ausdrücklich von Fremdinhalten jeglicher Art!